summaryrefslogtreecommitdiffstats
path: root/bash/tools/common-functions.sh
blob: 4ccb20697962534351401d80ef6cd24fc2935a91 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#   COPYRIGHT NOTICE STARTS HERE
#
#   Copyright 2018 © Samsung Electronics Co., Ltd.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#   COPYRIGHT NOTICE ENDS HERE

#
# this file contains shared variables and functions for the onap installer
#

# any script which needs this file can check this variable
# and it will know immediately if the functions and variables
# are loaded and usable
IS_COMMON_FUNCTIONS_SOURCED=YES

# setting of the path variables
if [ -z "$APROJECT_DIR" ] ; then
    INCLUDE_PATH="${LOCAL_PATH}"/"${RELATIVE_PATH}"
    APROJECT_DIR=$(readlink -f "$INCLUDE_PATH"/../..)
fi

RESOURCES_DIR="$APROJECT_DIR/resources"
BASH_SCRIPTS_DIR="$APROJECT_DIR/bash"
NEXUS_DATA="$RESOURCES_DIR/nexus_data"
CERTS_TARGET_PATH="$APROJECT_DIR/live/certs"
NGINX_LOG_DIR="$APROJECT_DIR/live/nginx_logs"
GEN_CFG_PATH="$APROJECT_DIR/live/cfg"
GIT_REPOS="$RESOURCES_DIR/git-repo"
NGINX_HTTP_DIR="$RESOURCES_DIR/http"
RHEL_REPO="$RESOURCES_DIR/pkg/rhel"

PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
export PATH

# just self-defense against locale
LANG=C
export LANG

# dns handling
SIMUL_HOSTS="gcr.io \
git.rancher.io \
gerrit.onap.org \
registry-1.docker.io \
docker.io \
registry.npmjs.org \
nexus3.onap.org \
nexus.onap.org \
docker.elastic.co \
www.getcloudify.org \
www.springframework.org \
registry.hub.docker.com \
git.onap.org \
repo1.maven.org \
repo.maven.apache.org"

# default credentials to the repository
NEXUS_USERNAME=admin
NEXUS_PASSWORD=admin123
NEXUS_EMAIL=admin@onap.org

# this function is intended to unify the installer output
message() {
    case "$1" in
        info)
            echo 'INFO:' "$@"
            ;;
        debug)
            echo 'DEBUG:' "$@" >&2
            ;;
        warning)
            echo 'WARNING [!]:' "$@" >&2
            ;;
        error)
            echo 'ERROR [!!]:' "$@" >&2
            return 1
            ;;
        *)
            echo 'UNKNOWN [?!]:' "$@" >&2
            return 2
            ;;
    esac
    return 0
}
export message

# if the environment variable DEBUG is set to DEBUG-ONAP ->
#  -> this function will print its arguments
# otherwise nothing is done
debug() {
    [ "$DEBUG" = DEBUG-ONAP ] && message debug "$@"
}
export debug

fail() {
    message error "$@"
    exit 1
}

retry() {
    local n=1
    local max=5
    while ! "$@"; do
        if [ $n -lt $max ]; then
            n=$((n + 1))
            message warning "Command ${@} failed. Attempt: $n/$max"
            message info "waiting 10s for another try..."
            sleep 10s
        else
            fail "Command ${@} failed after $n attempts. Better to abort now."
        fi
    done
}

may_self_extract() {
    # extract and untar to the current directory
    sed '0,/^# PAYLOAD BELOW #$/d' "$0" | tar -xvpf - ;
}

update_hosts() {
    if grep -q "^[^#]\+\s$SIMUL_HOSTS\s*\$" /etc/hosts ; then
        message info "simulated domains already in /etc/hosts"
    else
        echo "$LOCAL_IP $SIMUL_HOSTS" >> /etc/hosts
        message info "simulated domains added to /etc/hosts (please check it)"
    fi

    if grep -q "^[^#]\+\s$NEXUS_FQDN\s*\$" /etc/hosts ; then
        message info "nexus FQDN already in /etc/hosts"
    else
        echo "$LOCAL_IP $NEXUS_FQDN" >> /etc/hosts
        message info "Nexus FQDN added to /etc/hosts (please check it)"
    fi

    if grep -q "^[^#]\+\srepo.install-server\s*\$" /etc/hosts ; then
        message info "custom repo FQDN already in /etc/hosts"
    else
        echo "$LOCAL_IP repo.install-server" >> /etc/hosts
        message info "Nexus FQDN added to /etc/hosts (please check it)"
    fi
}

get_cfg_val() {
    name="$1"
    shift
    ask="$@"

    value=$(eval "echo \$${name}")
    if [ -z "$value" ]; then
        while [ -z "$value" ] ; do
            printf "${ask}"
            read -r $name

            value=$(eval "echo \$${name}")
        done
        echo "${name}='${value}'" >> ./local_repo.conf
    fi
}

get_configuration() {
    if [ -f ./local_repo.conf ]; then
        . ./local_repo.conf
    fi

    if [ -z "${NEXUS_FQDN}" ]; then
        NEXUS_FQDN="nexus.$HOSTNAME"
        echo "NEXUS_FQDN='${NEXUS_FQDN}'" >> ./local_repo.conf
    fi

    if [ -z "${ONAP_SCALE}" ]; then
        ONAP_SCALE=full
        echo "ONAP_SCALE='${ONAP_SCALE}'" >> ./local_repo.conf
    fi

    # nexus should be configured using those default entries
    # if it was not put the correct inputs instead
    if [ -z "${NPM_USERNAME}" ]; then
        NPM_USERNAME="${NEXUS_USERNAME}"
        echo "NPM_USERNAME='${NPM_USERNAME}'" >> ./local_repo.conf
    fi

    if [ -z "${NPM_PASSWORD}" ]; then
        NPM_PASSWORD="${NEXUS_PASSWORD}"
        echo "NPM_PASSWORD='${NPM_PASSWORD}'" >> ./local_repo.conf
    fi

    if [ -z "${NPM_EMAIL}" ]; then
        NPM_EMAIL="$NEXUS_EMAIL"
        echo "NPM_EMAIL='${NPM_EMAIL}'" >> ./local_repo.conf
    fi

    export NEXUS_FQDN
    export ONAP_SCALE
    export NPM_USERNAME
    export NPM_PASSWORD
    export NPM_EMAIL

    NODE_USERNAME="root"

    if [ -z "$LOCAL_IP" ] ; then
        echo
        echo "======= Mandatory configuration ======="
        echo
        message info "fill in these mandatory configuration values"
        get_cfg_val "LOCAL_IP" "Enter the public IPv4 used for this '$HOSTNAME' install machine," \
            "\nDO NOT USE LOOPBACK! (for example: 10.0.0.1): "
    fi
}

enable_local_repo() {
    sed -r "s%PATH%file://$APROJECT_DIR/resources/pkg/rhel%" "$APROJECT_DIR/resources/pkg/rhel/onap.repo" > /etc/yum.repos.d/onap.repo
}

install_packages() {
    os_id="$1"

    message info "Installing packages"

    case "$os_id" in
        centos)
            yum -y install "$APROJECT_DIR/resources/pkg/centos/*.rpm"
            ;;
        rhel)
            enable_local_repo
            yum -y install docker-ce dnsmasq icewm firefox tigervnc-server
            systemctl enable docker
            systemctl start docker
            ;;
        ubuntu)
            dpkg -i "$APROJECT_DIR/resources/pkg/ubuntu/*.deb"
            ;;
        *)
            message error "OS release is not supported: $os_id"
            message info "ABORTING INSTALLATION"
            exit 1
            ;;
    esac
}

install_files() {
    message info "installation of external binaries"
    for binary in kubectl helm rancher jq ; do
        cp "$APROJECT_DIR/resources/downloads/${binary}" /usr/local/bin/
        chmod 755 "/usr/local/bin/${binary}"
    done
    mkdir ~/.kube
}

setup_vnc_server() {
    mkdir -p ~/.vnc ~/.icewm
    echo "onap" | vncpasswd -f > ~/.vnc/passwd
    chmod 0600 ~/.vnc/passwd

    cat > ~/.vnc/xstartup <<EOF
#!/bin/sh

unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec icewm-session

EOF

chmod +x ~/.vnc/xstartup

    cat > ~/.icewm/menu <<EOF
prog Firefox firefox firefox
separator

EOF
vncserver
}

update_docker_cfg() {
    if [ -f "/etc/docker/daemon.json" ]; then
        jq '.dns += ["172.17.0.1"]' /etc/docker/daemon.json > /tmp/daemon.json
        mv /tmp/daemon.json /etc/docker/daemon.json
    else
        echo '{"dns": ["172.17.0.1"]}' > /etc/docker/daemon.json
    fi
}

create_root_CA() {
    echo "** Generate certificates **"
    openssl genrsa -out $CERTS_TARGET_PATH/rootCA.key 4096

    echo "** Generate self signed ***"
    openssl req -config $GEN_CFG_PATH/cacert.cnf -key $CERTS_TARGET_PATH/rootCA.key -new -x509 -days 7300 -sha256 -extensions v3_ca \
                  -out $CERTS_TARGET_PATH/rootCAcert.pem


    # convert to crt
    openssl x509 -in $CERTS_TARGET_PATH/rootCAcert.pem -inform PEM -out $CERTS_TARGET_PATH/rootCAcert.crt
}

install_root_CA() {
    os=$1
    echo "** Publishing root CA **"
    if [ "$os" == "redhat" ]; then
        # for centos
        update-ca-trust force-enable
        cp $CERTS_TARGET_PATH/rootCAcert.crt /etc/pki/ca-trust/source/anchors/
        update-ca-trust extract
    elif [ "$os" == "ubuntu" ]; then
        mkdir -p /usr/local/share/ca-certificates/extra
        cp $CERTS_TARGET_PATH/rootCAcert.crt /usr/local/share/ca-certificates/extra
        update-ca-certificates
    else
        echo "OS \"$os\" is not supported"
        exit -2
    fi

    echo "** Restart docker (because of reload new CA) **"
    systemctl restart docker

}

create_cert() {
    server_name=$1

    openssl genrsa -out $CERTS_TARGET_PATH/${server_name}_server.key 4096
    echo "** Generate sig request ***"
    openssl req -new -config $GEN_CFG_PATH/${server_name}_cert.cnf -key $CERTS_TARGET_PATH/${server_name}_server.key -out $CERTS_TARGET_PATH/${server_name}_server.csr

    # v3.ext must be in separate file , because of bug in openssl 1.0
    echo "** sign **"
    openssl x509 -req -in $CERTS_TARGET_PATH/${server_name}_server.csr\
    -extfile $GEN_CFG_PATH/v3.ext\
    -CA $CERTS_TARGET_PATH/rootCAcert.crt\
    -CAkey $CERTS_TARGET_PATH/rootCA.key\
    -CAcreateserial -out $CERTS_TARGET_PATH/${server_name}_server.crt -days 3650 -sha256
}

create_all_certs() {
    create_cert "nexus"
}

update_firewall() {
# TODO
#firewall-cmd --permanent --add-port=53/udp
#firewall-cmd --permanent --add-port=53/tcp
#firewall-cmd --permanent --add-port=10001/tcp
#firewall-cmd --permanent --add-port=80/tcp
#firewall-cmd --permanent --add-port=443/tcp
return 0
}

distribute_root_CA() {
    targetip=$1
    scp $APROJECT_DIR/install_cacert.sh $targetip:.
    ssh $targetip ./install_cacert.sh
    echo "** Add DNS record to remote host **"
    ssh $targetip "echo nameserver $LOCAL_IP > /etc/resolv.conf"
}

upload_ansible_pkgs() {
    os=$1
    targetip=$2
    #if [[ $os == "ubuntu" ]]; then
    # those deb & whl packages are needed for sdnc-ansible-server pod
    # independently on host OS distros
    echo "** Copy required packages for sdnc-ansible-pod to kubernetes node $targetip **"
    scp -r $APROJECT_DIR/resources/pkg/ubuntu/ansible_pkg $targetip:.
    #fi
}

remote_setup_nfs_server() {
    os=$1
    targetip=$2
    shift 2
    scp $APROJECT_DIR/bash/tools/setup_nfs_server_${os}.sh $targetip:setup_nfs_server.sh
    if [[ $os == "ubuntu" ]]; then
        scp -r $APROJECT_DIR/resources/pkg/ubuntu/nfs-common-pkg/* $targetip:.
        ssh $targetip dpkg -i *.deb
    fi

    ssh $targetip /bin/bash ./setup_nfs_server.sh "$@"
}

remote_setup_nfs_mount() {
    os=$1
    targetip=$2
    nfsip=$3
    scp $APROJECT_DIR/bash/tools/setup_nfs_mount.sh $targetip:.
    if [[ $os == "ubuntu" ]]; then
        scp -r $APROJECT_DIR/resources/pkg/ubuntu/nfs-common-pkg/* $targetip:.
        ssh $targetip dpkg -i *.deb
    fi
    ssh $targetip /bin/bash ./setup_nfs_mount.sh $nfsip
}

enable_remote_repo() {
    targetip=$1
    sed -r "s%PATH%http://repo.install-server%" $APROJECT_DIR/resources/pkg/rhel/onap.repo | ssh $targetip 'cat > /etc/yum.repos.d/onap.repo'
}

install_remote_docker() {
    targetip=$1
    os=$2
    if [[ $os == "ubuntu" ]]; then
        scp -r $APROJECT_DIR/resources/pkg/ubuntu/{docker-ce_17.03.2~ce-0~ubuntu-xenial_amd64.deb,libltdl7_2.4.6-0.1_amd64.deb} $targetip:.
        ssh $targetip dpkg -i *.deb
    elif [[ $os == "rhel" ]]; then
        ssh $targetip yum -y install docker-ce
    fi
    ssh $targetip "mkdir -p /etc/docker"
    scp "$APROJECT_DIR/resources/downloads/jq" $targetip:/usr/local/bin/
    ssh $targetip "if [[ -f /etc/docker/daemon.json ]]; then
                       jq '.dns += [\"$LOCAL_IP\"]' /etc/docker/daemon.json > /tmp/daemon.json
                       mv /tmp/daemon.json /etc/docker/daemon.json
                   else
                       echo {'\"'dns'\"': ['\"'$LOCAL_IP'\"']} > /etc/docker/daemon.json
                   fi"

    ssh $targetip 'systemctl enable docker; systemctl restart docker'
}

deploy_rancher() {
    docker run -d --entrypoint "/bin/bash" --restart=unless-stopped -p 8080:8080 \
    -v $CERTS_TARGET_PATH:/usr/local/share/ca-certificates/extra:ro \
    --name rancher_server rancher/server:v1.6.14 \
    -c "/usr/sbin/update-ca-certificates;/usr/bin/entry /usr/bin/s6-svscan /service"
    echo "** wait until rancher is ready **"
}

deploy_kubernetes() {
    os=$1
    set +e
    for i in `seq 5 -1 1`; do
        API_RESPONSE=`curl -s 'http://127.0.0.1:8080/v2-beta/apikey' \
            -d '{"type":"apikey","accountId":"1a1","name":"autoinstall"\
                 ,"description":"autoinstall","created":null,"kind":null,\
                 "removeTime":null,"removed":null,"uuid":null}'`
        if [[ "$?" -eq 0 ]]; then
            KEY_PUBLIC=`echo $API_RESPONSE | jq -r .publicValue`
            KEY_SECRET=`echo $API_RESPONSE | jq -r .secretValue`
            break
        fi
        echo "Waiting for rancher server to start"
        sleep 60
    done
    set -e
    export RANCHER_URL=http://${LOCAL_IP}:8080
    export RANCHER_ACCESS_KEY=$KEY_PUBLIC
    export RANCHER_SECRET_KEY=$KEY_SECRET

    rancher env ls
    echo "wait 60 sec for rancher environments can settle before we create the onap kubernetes one"
    sleep 60

    rancher env create -t kubernetes onap > kube_env_id.json
    PROJECT_ID=$(<kube_env_id.json)
    echo "env id: $PROJECT_ID"
    export RANCHER_HOST_URL=http://${LOCAL_IP}:8080/v1/projects/$PROJECT_ID

    for i in `seq 5`; do
        status=$(rancher env ls | grep $PROJECT_ID | awk '{print $4}')
        if [[ "$status" == "active" ]]; then
            echo "Check on environments again before registering the URL response"
            rancher env ls
            break
        fi
        echo "Wait for environment to become active"
        sleep 30
    done

    REG_URL_RESPONSE=`curl -X POST -u $KEY_PUBLIC:$KEY_SECRET -H 'Accept: application/json' -H 'ContentType: application/json' -d '{"name":"$LOCAL_IP"}' "http://$LOCAL_IP:8080/v1/projects/$PROJECT_ID/registrationtokens"`
    echo "wait for server to finish url configuration - 3 min"
    sleep 180
    # see registrationUrl in
    REGISTRATION_TOKENS=`curl http://127.0.0.1:8080/v2-beta/registrationtokens`
    REGISTRATION_DOCKER=`echo $REGISTRATION_TOKENS | jq -r .data[0].image`
    REGISTRATION_TOKEN=`echo $REGISTRATION_TOKENS | jq -r .data[0].token`

    # base64 encode the kubectl token from the auth pair
    # generate this after the host is registered
    KUBECTL_TOKEN=$(echo -n 'Basic '$(echo -n "$RANCHER_ACCESS_KEY:$RANCHER_SECRET_KEY" | base64 -w 0) | base64 -w 0)
    echo "KUBECTL_TOKEN base64 encoded: ${KUBECTL_TOKEN}"
    cat > ~/.kube/config <<EOF
apiVersion: v1
kind: Config
clusters:
- cluster:
    api-version: v1
    insecure-skip-tls-verify: true
    server: "https://$LOCAL_IP:8080/r/projects/$PROJECT_ID/kubernetes:6443"
  name: "onap"
contexts:
- context:
    cluster: "onap"
    user: "onap"
  name: "onap"
current-context: "onap"
users:
- name: "onap"
  user:
    token: "$KUBECTL_TOKEN"

EOF

    if [[ $os == "rhel" ]]; then
        echo "Upgrade datavolume for RHEL"
        KUBELET_ID=`curl http://${LOCAL_IP}:8080/v2-beta/projects/${PROJECT_ID}/services/ | jq -r '.data[] | select(.name=="kubelet")'.id`
        OLD_LAUNCH_CONFIG=`curl http://${LOCAL_IP}:8080/v2-beta/projects/${PROJECT_ID}/services/${KUBELET_ID} | jq  '.launchConfig'`
        NEW_LAUNCH_CONFIG=`echo $OLD_LAUNCH_CONFIG | jq '.dataVolumes[2]="/sys/fs/cgroup:/sys/fs/cgroup:ro,rprivate"'`

        DATA="{
                \"inServiceStrategy\": {
                  \"batchSize\": 1,
                  \"intervalMillis\": 2000,
                  \"startFirst\": false,
                  \"launchConfig\": ${NEW_LAUNCH_CONFIG},
                  \"secondaryLaunchConfigs\": []
                }
        }"
        curl -s -u $KEY_PUBLIC:$KEY_SECRET -X POST -H 'Content-Type: application/json' -d "${DATA}" "http://${LOCAL_IP}:8080/v2-beta/projects/${PROJECT_ID}/services/${KUBELET_ID}?action=upgrade" > /dev/null

        echo "Give environment time to update (30 sec)"
        sleep 30

        curl -s -u $KEY_PUBLIC:$KEY_SECRET -X POST "http://${LOCAL_IP}:8080/v2-beta/projects/${PROJECT_ID}/services/${KUBELET_ID}?action=finishupgrade" > /dev/null
    fi
}

deploy_rancher_agent() {
    nodeip=$1
    if [ -z "$REGISTRATION_DOCKER" ]; then
        echo "ASSERT: Missing REGISTRATION_DOCKER"
        exit 1
    fi
    if [ -z "$RANCHER_URL" ]; then
        echo "ASSERT: Missing RANCHER_URL"
        exit 1
    fi
    if [ -z "$REGISTRATION_TOKEN" ]; then
        echo "ASSERT: Missing REGISTRATION_TOKEN"
        exit 1
    fi

    ssh $nodeip "docker run --rm --privileged -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/racher:/var/lib/rancher $REGISTRATION_DOCKER $RANCHER_URL/v1/scripts/$REGISTRATION_TOKEN"
    echo "waiting 2 min for creating kubernetes environment"
    sleep 120
}

deploy_node() {
    nodeip=$1
    os=$2
    echo "Deploying node $nodeip"
    distribute_root_CA $nodeip
    install_remote_docker $nodeip $os
    deploy_rancher_agent $nodeip
}

deploy_onap() {
    pushd $APROJECT_DIR/resources/oom/kubernetes
    helm init --upgrade --skip-refresh
    # this might fail
    set +e
    helm repo remove stable
    set -e
    helm serve &
    echo "wait a moment before helm will come up ..."
    sleep 5
    helm repo add local http://127.0.0.1:8879
    make all
    #Pass the CA certificate contents directly during installation.
    helm install local/onap -n dev --namespace onap \
      --set "global.cacert=$(cat ${CERTS_TARGET_PATH}/rootCAcert.crt)"
    popd
}

expand_file() {
    file=$1
    # print warning if patched file does not exist as some charts
    # might not be available for some deployments
    if [ ! -f "$file" ]; then
      echo "WARNING: Can't patch file $file because this file does not exists."
      return 0
    fi

    shift

    for ivar in "$@" ; do
        ivalue=$(eval 'echo "$'${ivar}'"')
        sed -i "s#${ivar}#${ivalue}#g" "$file"
    done
}

patch_npm_oom() {
    if [ -z "$LOCAL_IP" ] ; then
        echo "ERROR: LOCAL_IP unset"
        return 1
    fi
    if [ -z "$NEXUS_FQDN" ] ; then
        echo "ERROR: NEXUS_FQDN unset"
        return 1
    fi

    UPDATE_HOSTS_FILE="$LOCAL_IP $NEXUS_FQDN"
    UPDATE_NPM_REGISTRY="npm set registry \"http://${NEXUS_FQDN}/repository/npm-private/\""

    expand_file $APROJECT_DIR/resources/oom/kubernetes/common/dgbuilder/templates/deployment.yaml \
        UPDATE_HOSTS_FILE \
        UPDATE_NPM_REGISTRY
    expand_file $APROJECT_DIR/resources/oom/kubernetes/sdnc/charts/sdnc-portal/templates/deployment.yaml \
        UPDATE_HOSTS_FILE \
        UPDATE_NPM_REGISTRY
}

patch_spring_oom() {
    if [ -z "$LOCAL_IP" ] ; then
        echo "ERROR: LOCAL_IP unset"
        return 1
    fi

    UPDATE_HOSTS_FILE="$LOCAL_IP www.springframework.org"
    expand_file $APROJECT_DIR/resources/oom/kubernetes/dmaap/charts/message-router/templates/deployment.yaml \
        UPDATE_HOSTS_FILE
}

patch_cfy_manager_depl() {
    os="$1"
    file="${APROJECT_DIR}/resources/oom/kubernetes/dcaegen2/charts/dcae-cloudify-manager/templates/deployment.yaml"

    case "$os" in
        centos|rhel)
            CERT_PATH="/etc/pki/ca-trust/source/anchors"
            ;;
        ubuntu)
            CERT_PATH="/usr/local/share/ca-certificates/extra"
            ;;
        '')
            echo "ERROR: missing argument"
            return 1
            ;;
        *)
            echo "ERROR: unknown OS: ${os}"
            return 1
            ;;
    esac

    expand_file "$file" CERT_PATH
}

copy_onap_values_file() {
    cp "${APROJECT_DIR}/${CUSTOM_CFG_RELPATH:-cfg}/${ONAP_SCALE}_depl_values.yaml" \
        "${APROJECT_DIR}/resources/oom/kubernetes/onap/values.yaml"
}