| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 1 | #! /bin/bash |
| tierno | d125caf | 2018-11-22 16:05:54 +0000 | [diff] [blame] | 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 16 | # author: Alfonso Tierno |
| 17 | # Script that uses the test NBI URL to clean database. See usage |
| 18 | |
| 19 | |
| 20 | function usage(){ |
| 21 | echo -e "usage: $0 [OPTIONS]" |
| tierno | 8907c4d | 2019-06-25 11:15:32 +0000 | [diff] [blame] | 22 | echo -e "TEST NBI API is used to clean database content, except admin users,projects and roles. Useful for testing." |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 23 | echo -e "NOTE: database is cleaned but not the content of other modules as RO or VCA that must be cleaned manually." |
| 24 | echo -e " OPTIONS" |
| 25 | echo -e " -h --help: show this help" |
| 26 | echo -e " -f --force: Do not ask for confirmation" |
| tierno | 8907c4d | 2019-06-25 11:15:32 +0000 | [diff] [blame] | 27 | echo -e " --completely: It cleans also admin user/project and roles. It only works for internal" \ |
| 28 | "authentication. NBI will need to be restarted to init database" |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 29 | echo -e " --clean-RO: clean RO content. RO client (openmano) must be installed and configured" |
| 30 | echo -e " --clean-VCA: clean VCA content. juju must be installed and configured" |
| 31 | echo -e " ENV variable 'OSMNBI_URL' is used for the URL of the NBI server. If missing, it uses" \ |
| 32 | "'https://\$OSM_HOSTNAME:9999/osm'. If 'OSM_HOSTNAME' is missing, localhost is used" |
| 33 | } |
| 34 | |
| 35 | |
| 36 | function ask_user(){ |
| 37 | # ask to the user and parse a response among 'y', 'yes', 'n' or 'no'. Case insensitive. |
| 38 | # Params: $1 text to ask; $2 Action by default, can be 'y' for yes, 'n' for no, other or empty for not allowed. |
| 39 | # Return: true(0) if user type 'yes'; false (1) if user type 'no' |
| 40 | read -e -p "$1" USER_CONFIRMATION |
| 41 | while true ; do |
| 42 | [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'y' ] && return 0 |
| 43 | [ -z "$USER_CONFIRMATION" ] && [ "$2" == 'n' ] && return 1 |
| 44 | [ "${USER_CONFIRMATION,,}" == "yes" ] || [ "${USER_CONFIRMATION,,}" == "y" ] && return 0 |
| 45 | [ "${USER_CONFIRMATION,,}" == "no" ] || [ "${USER_CONFIRMATION,,}" == "n" ] && return 1 |
| 46 | read -e -p "Please type 'yes' or 'no': " USER_CONFIRMATION |
| 47 | done |
| 48 | } |
| 49 | |
| 50 | |
| 51 | while [ -n "$1" ] |
| 52 | do |
| 53 | option="$1" |
| 54 | shift |
| 55 | ( [ "$option" == -h ] || [ "$option" == --help ] ) && usage && exit |
| 56 | ( [ "$option" == -f ] || [ "$option" == --force ] ) && OSMNBI_CLEAN_FORCE=yes && continue |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 57 | [ "$option" == --completely ] && OSMNBI_COMPLETELY=yes && continue |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 58 | [ "$option" == --clean-RO ] && OSMNBI_CLEAN_RO=yes && continue |
| 59 | [ "$option" == --clean-VCA ] && OSMNBI_CLEAN_VCA=yes && continue |
| 60 | echo "Unknown option '$option'. Type $0 --help" 2>&1 && exit 1 |
| 61 | done |
| 62 | |
| 63 | |
| 64 | [ -n "$OSMNBI_CLEAN_FORCE" ] || ask_user "Clean database content (y/N)?" n || exit |
| 65 | [ -z "$OSM_HOSTNAME" ] && OSM_HOSTNAME=localhost |
| 66 | [ -z "$OSMNBI_URL" ] && OSMNBI_URL="https://${OSM_HOSTNAME}:9999/osm" |
| 67 | |
| 68 | if [ -n "$OSMNBI_CLEAN_RO" ] |
| 69 | then |
| 70 | export OPENMANO_TENANT=osm |
| 71 | for dc in `openmano datacenter-list | awk '{print $1}'` |
| 72 | do |
| 73 | export OPENMANO_DATACENTER=$dc |
| 74 | for i in instance-scenario scenario vnf |
| 75 | do |
| 76 | for f in `openmano $i-list | awk '{print $1}'` |
| 77 | do |
| 78 | [[ -n "$f" ]] && [[ "$f" != No ]] && openmano ${i}-delete -f ${f} |
| 79 | done |
| 80 | done |
| 81 | done |
| 82 | fi |
| 83 | |
| tierno | 1546f2a | 2019-08-20 15:38:11 +0000 | [diff] [blame] | 84 | for item in vim_accounts wim_accounts sdns nsrs vnfrs nslcmops nsds vnfds pdus nsts nsis nsilcmops # vims |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 85 | do |
| 86 | curl --insecure ${OSMNBI_URL}/test/db-clear/${item} |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 87 | done |
| tierno | 8907c4d | 2019-06-25 11:15:32 +0000 | [diff] [blame] | 88 | curl --insecure ${OSMNBI_URL}/test/fs-clear |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 89 | if [ -n "$OSMNBI_COMPLETELY" ] ; then |
| tierno | 8907c4d | 2019-06-25 11:15:32 +0000 | [diff] [blame] | 90 | # delete all users. It will only works for internal backend |
| tierno | f717cbe | 2018-12-03 16:35:42 +0000 | [diff] [blame] | 91 | curl --insecure ${OSMNBI_URL}/test/db-clear/users |
| tierno | 8907c4d | 2019-06-25 11:15:32 +0000 | [diff] [blame] | 92 | curl --insecure ${OSMNBI_URL}/test/db-clear/projects |
| 93 | curl --insecure ${OSMNBI_URL}/test/db-clear/roles |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 94 | fi |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 95 | |
| tierno | 8907c4d | 2019-06-25 11:15:32 +0000 | [diff] [blame] | 96 | [[ -z "$OSM_USER" ]] && OSM_USER=admin |
| 97 | [[ -z "$OSM_PASSWORD" ]] && OSM_PASSWORD=admin |
| 98 | [[ -z "$OSM_PROJECT" ]] && OSM_PROJECT=admin |
| 99 | |
| 100 | TOKEN=`curl --insecure -H "Content-Type: application/yaml" -H "Accept: application/yaml" \ |
| 101 | --data "{username: '$OSM_USER', password: '$OSM_PASSWORD', project_id: '$OSM_PROJECT'}" \ |
| 102 | ${OSMNBI_URL}/admin/v1/tokens 2>/dev/null | awk '($1=="_id:"){print $2}'`; |
| 103 | echo "TOKEN='$TOKEN'" |
| 104 | |
| 105 | echo "delete users, prujects,roles. Ignore response errors due that own user,project cannot be deleted" |
| 106 | for topic in users projects roles |
| 107 | do |
| 108 | elements=`curl --insecure ${OSMNBI_URL}/admin/v1/$topic -H "Authorization: Bearer $TOKEN" \ |
| 109 | -H "Accept: application/yaml" 2>/dev/null | awk '($1=="_id:"){print $2};($2=="_id:"){print $3}'`; |
| 110 | for element in $elements |
| 111 | do |
| 112 | # not needed to check if own user, project, etc; because OSM will deny deletion |
| 113 | echo deleting $topic _id=$element |
| 114 | curl --insecure ${OSMNBI_URL}/admin/v1/$topic/$element -H "Authorization: Bearer $TOKEN" \ |
| 115 | -H "Accept: application/yaml" -X DELETE 2>/dev/null |
| 116 | done |
| 117 | done |
| 118 | |
| 119 | |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 120 | if [ -n "$OSMNBI_CLEAN_RO" ] |
| 121 | then |
| 122 | for dc in `openmano datacenter-list | awk '{print $1}'` ; do openmano datacenter-detach $dc ; done |
| 123 | for dc in `openmano datacenter-list --all | awk '{print $1}'` ; do openmano datacenter-delete -f $dc ; done |
| 124 | for dc in `openmano sdn-controller-list | awk '{print $1}'` ; do openmano sdn-controller-delete -f $dc ; done |
| tierno | 55ba2e6 | 2018-12-11 17:22:22 +0000 | [diff] [blame] | 125 | for dc in `openmano wim-list | awk '{print $1}'` ; do openmano wim-detach $dc ; done |
| 126 | for dc in `openmano wim-list --all | awk '{print $1}'` ; do openmano wim-delete -f $dc ; done |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 127 | fi |
| 128 | |
| 129 | if [ -n "$OSMNBI_CLEAN_VCA" ] |
| 130 | then |
| tierno | 3b438be | 2019-05-31 13:41:31 +0000 | [diff] [blame] | 131 | for juju_model in `juju models | grep lxd | grep -v controller | grep -v default | awk '{print$1}'` |
| 132 | do |
| 133 | echo |
| 134 | echo juju destroy-model $juju_model |
| 135 | juju destroy-model -y $juju_model |
| 136 | done |
| 137 | # juju destroy-model -y default |
| 138 | # juju add-model default |
| tierno | cd54a4a | 2018-09-12 16:40:35 +0200 | [diff] [blame] | 139 | fi |