Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions elasticsearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ Example:
```
$ oc exec -c elasticsearch $POD -- allocate-stale-primary .kibana.02c55f18a892b365bcd1802db9e5c9df39c04674
```
### export-kibana-objects
Export kibana objects for a given user from their kibana index as a JSON search response

Example:
```
$ oc exec -c elasticsearch $POD -- export-kibana-objects admin2 > kib.json
```
### import-kibana-objects
Import kibana objects from a JSON search response produced by `export-kibana-objects` to the kibana index in the
input file.

Example:
```
$ cat kib.json | oc exec -i -c elasticsearch $pod -- import-kibana-objects
```

### logs
Retrieve Elasticsearch logs from the log directory. This command defaults to retrieving
Expand Down
62 changes: 62 additions & 0 deletions elasticsearch/utils/export-kibana-objects
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
#
# Copyright 2019 Red Hat, Inc. and/or its affiliates
# and other contributors as indicated by the @author tags.
#
# 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.

set -euo pipefail
source es_util_env
source logging

function usage() {
cat << EOF

Exports a given user's kibana objects from their unique
kibana index and write the results to STDOUT as an Elasticsearch
search response.

usage:
$0 <username> [objects]

username the user's name or '\$\$kibana' if it should retrieve
the '.kibana' objects
objects a comma delimited list of objects (Defaults: visualization,dashboard,search)

EOF
exit 1
}

if [ -z "${1:-}" ] ; then
usage
fi

username=$1
OBJECTS=${2:-visualization,dashboard,search}
CMD=${CMD:-es_util}
kibindex=$( get_kibana_index_name "${username}" )

if [ "$username" == '$$kibana' ] ; then
kibindex=.kibana
fi

resp_code=$(${CMD} --query="$kibindex" \
--request HEAD --head --output /dev/null \
-w '%{response_code}' )

if [ "$resp_code" != 200 ] ; then
error Could not find kibana index ${kibindex} for user ${username}: $resp_code
exit 1
fi

${CMD} --query="${kibindex}/${OBJECTS}/_search?pretty"
83 changes: 83 additions & 0 deletions elasticsearch/utils/import-kibana-objects
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash
#
# Copyright 2019 Red Hat, Inc. and/or its affiliates
# and other contributors as indicated by the @author tags.
#
# 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.

set -euo pipefail
source es_util_env

function usage() {
cat << EOF

Import kibana objects from a search response to the kibana index found in the first entry of
a search response exported by 'export-kibana-objects'. NOTE: This script
overwrites/updates any objects of the same type and id

usage:
$0 <objects_json>

objects_json A search response JSON from the export script

EOF
exit "1"
}

while IFS= read -r -t 5 line
do
file="${file:-}${line}"
done < "${1:-/dev/stdin}"

if [ -z "${file:-}" ] ; then
usage
fi


CMD=${CMD:-es_util}
kibindex=$(
echo "$file" | python -c '
import sys
import json
obj = json.load(sys.stdin)
if obj["hits"]["total"] == 0:
sys.stdout.write("")
sys.stdout.write(obj["hits"]["hits"][0]["_index"])
'
)
query="${kibindex}/_bulk"
response=$({
echo "$file" | python -c '
import sys
import json
obj = json.load(sys.stdin)
for doc in obj["hits"]["hits"]:
hdr = {"index":{"_type":doc["_type"],"_id":doc["_id"]}}
json.dump(hdr, sys.stdout)
sys.stdout.write("\n")
json.dump(doc["_source"], sys.stdout)
sys.stdout.write("\n")
'
} | ${CMD} --query="${query}" -H "Content-type: application/json" -XPOST --data-binary @-)

echo $response | python -c '
import sys
import json
obj = json.load(sys.stdin)
if obj["errors"]:
sys.stdout.write("There were errors importing objects:\n")
sys.stdout.write(json.dumps(obj, indent=4))
sys.stdout.write("\n")
else:
sys.stdout.write("Success\n")
'