blob: 48a5e5a40d52718c76371ee96b1218d5d3ff106a [file] [log] [blame]
ebo3ecb7b32020-02-27 14:04:23 +00001#!/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
23set -o errexit
24set -o nounset
25set -o pipefail
26set -o xtrace
27
28export PATH=/opt/bin:/usr/local/bin:/usr/bin:/bin
29
30CONFIG=/config
ebo080e0452020-03-01 23:41:37 +000031SSH_CONFIG=$CONFIG/ssh
ebo3ecb7b32020-02-27 14:04:23 +000032TLS_CONFIG=$CONFIG/tls
33MODELS_CONFIG=$CONFIG/modules
34KEY_PATH=/opt/etc/keystored/keys
35BASE_VIRTUALENVS=$HOME/.local/share/virtualenvs
36
37find_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
48find_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
ebo080e0452020-03-01 23:41:37 +000059configure_ssh()
60{
61 sysrepocfg --datastore=startup --format=xml ietf-system --import=$SSH_CONFIG/load_auth_pubkey.xml
62}
63
ebo3ecb7b32020-02-27 14:04:23 +000064configure_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
72configure_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
86install_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
99configure_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]
112command=$prog $model
113redirect_stderr=true
114autorestart=true
115environment=PATH=$PROG_PATH,PYTHONPATH=/opt/lib/python3.7/site-packages,PYTHONUNBUFFERED="1"
116EOF
117}
118
119create_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
ebo080e0452020-03-01 23:41:37 +0000135configure_ssh
ebo3ecb7b32020-02-27 14:04:23 +0000136configure_tls
137configure_modules
138
139exec /usr/local/bin/supervisord -c /etc/supervisord.conf