ebo | 3ecb7b3 | 2020-02-27 14:04:23 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # shellcheck disable=SC2086 |
| 3 | |
| 4 | #- |
| 5 | # ============LICENSE_START======================================================= |
| 6 | # Copyright (C) 2020 Nordix Foundation. |
| 7 | # ================================================================================ |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
| 19 | # |
| 20 | # SPDX-License-Identifier: Apache-2.0 |
| 21 | # ============LICENSE_END========================================================= |
| 22 | |
| 23 | set -o errexit |
| 24 | set -o nounset |
| 25 | set -o pipefail |
| 26 | set -o xtrace |
| 27 | |
| 28 | export PATH=/opt/bin:/usr/local/bin:/usr/bin:/bin |
| 29 | |
| 30 | CONFIG=/config |
ebo | 080e045 | 2020-03-01 23:41:37 +0000 | [diff] [blame] | 31 | SSH_CONFIG=$CONFIG/ssh |
ebo | 3ecb7b3 | 2020-02-27 14:04:23 +0000 | [diff] [blame] | 32 | TLS_CONFIG=$CONFIG/tls |
| 33 | MODELS_CONFIG=$CONFIG/modules |
| 34 | KEY_PATH=/opt/etc/keystored/keys |
| 35 | BASE_VIRTUALENVS=$HOME/.local/share/virtualenvs |
| 36 | |
| 37 | find_file() { |
| 38 | local dir=$1 |
| 39 | shift |
| 40 | for prog in "$@"; do |
| 41 | if [ -f $dir/$prog ]; then |
| 42 | echo -n $dir/$prog |
| 43 | break |
| 44 | fi |
| 45 | done |
| 46 | } |
| 47 | |
| 48 | find_executable() { |
| 49 | local dir=$1 |
| 50 | shift |
| 51 | for prog in "$@"; do |
| 52 | if [ -x $dir/$prog ]; then |
| 53 | echo -n $dir/$prog |
| 54 | break |
| 55 | fi |
| 56 | done |
| 57 | } |
| 58 | |
ebo | 080e045 | 2020-03-01 23:41:37 +0000 | [diff] [blame] | 59 | configure_ssh() |
| 60 | { |
| 61 | sysrepocfg --datastore=startup --format=xml ietf-system --import=$SSH_CONFIG/load_auth_pubkey.xml |
| 62 | } |
| 63 | |
ebo | 3ecb7b3 | 2020-02-27 14:04:23 +0000 | [diff] [blame] | 64 | configure_tls() |
| 65 | { |
| 66 | cp $TLS_CONFIG/server_key.pem $KEY_PATH |
| 67 | cp $TLS_CONFIG/server_key.pem.pub $KEY_PATH |
| 68 | sysrepocfg --datastore=startup --format=xml ietf-keystore --merge=$TLS_CONFIG/load_server_certs.xml |
| 69 | sysrepocfg --datastore=startup --format=xml ietf-netconf-server --merge=$TLS_CONFIG/tls_listen.xml |
| 70 | } |
| 71 | |
| 72 | configure_modules() |
| 73 | { |
| 74 | for dir in "$MODELS_CONFIG"/*; do |
| 75 | if [ -d $dir ]; then |
| 76 | model=${dir##*/} |
| 77 | install_and_configure_yang_model $dir $model |
| 78 | prog=$(find_executable $dir subscriber.py) |
| 79 | if [ -n "$prog" ]; then |
| 80 | configure_subscriber_execution $dir $model $prog |
| 81 | fi |
| 82 | fi |
| 83 | done |
| 84 | } |
| 85 | |
| 86 | install_and_configure_yang_model() |
| 87 | { |
| 88 | local dir=$1 |
| 89 | local model=$2 |
| 90 | |
| 91 | yang=$(find_file $dir $model.yang model.yang) |
| 92 | sysrepoctl --install --yang=$yang |
| 93 | data=$(find_file $dir startup.json startup.xml data.json data.xml) |
| 94 | if [ -n "$data" ]; then |
| 95 | sysrepocfg --datastore=startup --import=$data $model |
| 96 | fi |
| 97 | } |
| 98 | |
| 99 | configure_subscriber_execution() |
| 100 | { |
| 101 | local dir=$1 |
| 102 | local model=$2 |
| 103 | local prog=$3 |
| 104 | |
| 105 | PROG_PATH=$PATH |
| 106 | if [ -r "$dir/requirements.txt" ]; then |
| 107 | env_dir=$(create_python_venv $dir) |
| 108 | PROG_PATH=$env_dir/bin:$PROG_PATH |
| 109 | fi |
| 110 | cat > /etc/supervisord.d/$model.conf <<EOF |
| 111 | [program:subs-$model] |
| 112 | command=$prog $model |
| 113 | redirect_stderr=true |
| 114 | autorestart=true |
| 115 | environment=PATH=$PROG_PATH,PYTHONPATH=/opt/lib/python3.7/site-packages,PYTHONUNBUFFERED="1" |
| 116 | EOF |
| 117 | } |
| 118 | |
| 119 | create_python_venv() |
| 120 | { |
| 121 | local dir=$1 |
| 122 | |
| 123 | mkdir -p $BASE_VIRTUALENVS |
| 124 | env_dir=$BASE_VIRTUALENVS/$model |
| 125 | ( |
| 126 | python3 -m venv --system-site-packages $env_dir |
| 127 | cd $env_dir |
| 128 | . ./bin/activate |
| 129 | pip install --upgrade pip |
| 130 | pip install -r "$dir"/requirements.txt |
| 131 | ) 1>&2 |
| 132 | echo $env_dir |
| 133 | } |
| 134 | |
ebo | 080e045 | 2020-03-01 23:41:37 +0000 | [diff] [blame] | 135 | configure_ssh |
ebo | 3ecb7b3 | 2020-02-27 14:04:23 +0000 | [diff] [blame] | 136 | configure_tls |
| 137 | configure_modules |
| 138 | |
| 139 | exec /usr/local/bin/supervisord -c /etc/supervisord.conf |