forked from fledge-iot/fledge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage
executable file
·104 lines (86 loc) · 3.35 KB
/
storage
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
#!/bin/bash
##--------------------------------------------------------------------
## Copyright (c) 2017-2018 OSIsoft, LLC
##
## 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.
##--------------------------------------------------------------------
#
# This script is used to call the PosegreSQL storage plugin script
# to store and retrieve the sensor data when the database
# is embedded in Fledge
#
#set -x
# Include common code
source "${FLEDGE_ROOT}/scripts/common/get_storage_plugin.sh"
source "${FLEDGE_ROOT}/scripts/common/get_readings_plugin.sh"
PLUGIN_TO_USE=""
# Logger wrapper
storage_log() {
write_log "Storage" "script.storage" "$1" "$2" "$3" "$4"
}
#############
## MAIN LOGIC
#############
PLUGIN_TO_USE=`get_storage_plugin`
READINGS_PLUGIN_TO_USE=`get_readings_plugin`
if [[ "${#PLUGIN_TO_USE}" -eq 0 ]]; then
storage_log "err" "Missing plugin from Fledge storage service" "all" "pretty"
fi
PLUGIN_SCRIPT="$FLEDGE_ROOT/scripts/plugins/storage/$PLUGIN_TO_USE.sh"
if [[ ! -x "$PLUGIN_SCRIPT" ]]; then
# Missing storage plugin script
storage_log "err" "Fledge cannot start." "all" "pretty"
storage_log "err" "Missing Storage Plugin script $PLUGIN_SCRIPT." "all" "pretty"
exit 1
fi
# The reset must be executed on both the storage and readings plugins, if the
# readings are stored in a different plugin. On the readings plugin this becomes
# a purge operation.
#
# The purge action is only executed via the readings plugin if defined, or
# the main storage plugin is not defined.
if [[ "$1" == "reset" ]] ; then
# Pass action in $1 and FLEDGE_VERSION in $2
source "$PLUGIN_SCRIPT" $1 $2
if [[ "$PLUGIN_TO_USE" != "$READINGS_PLUGIN_TO_USE" ]]; then
READINGS_SCRIPT="$FLEDGE_ROOT/scripts/plugins/storage/$READINGS_PLUGIN_TO_USE.sh"
if [[ -x "$READINGS_SCRIPT" ]]; then
source "$READINGS_SCRIPT" purge $2
fi
fi
elif [[ "$1" == "purge" ]]; then
# Pass action in $1 and FLEDGE_VERSION in $2
if [[ "$PLUGIN_TO_USE" != "$READINGS_PLUGIN_TO_USE" ]]; then
READINGS_SCRIPT="$FLEDGE_ROOT/scripts/plugins/storage/$READINGS_PLUGIN_TO_USE.sh"
# Soem readings plugins, notably sqlitememory, do not have a script
if [[ -x "$READINGS_SCRIPT" ]]; then
source "$READINGS_SCRIPT" $1 $2
fi
else
source "$PLUGIN_SCRIPT" $1 $2
fi
else
# Pass any other operation to the storage plugin
source "$PLUGIN_SCRIPT" $1 $2
# Also start the readings plugin if it is different to the configuration plugin
# The reason to do this is to create the schema in the readings database if required
if [[ "$PLUGIN_TO_USE" != "$READINGS_PLUGIN_TO_USE" ]]; then
READINGS_SCRIPT="$FLEDGE_ROOT/scripts/plugins/storage/$READINGS_PLUGIN_TO_USE.sh"
# Some readings plugins, notably sqlitememory, do not have a script
if [[ -x "$READINGS_SCRIPT" ]]; then
source "$READINGS_SCRIPT" $1 $2
fi
fi
fi
# exit cannot be used because the script is sourced.
#exit $?