Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add fully scripted influxdb + grafana installation #465

Merged
merged 1 commit into from
Dec 18, 2018
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
10 changes: 8 additions & 2 deletions functions/openhab.sh
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ openhab_is_installed() {
}

openhab_is_running() {
#TODO
exit 1
if [ `systemctl is-active openhab2` != "active" ]; then return 1; fi
if [ -r /etc/default/openhab2 ]; then
. /etc/default/openhab2
fi
# Read and set openHAB variables set in /etc/default/ scripts
if [ -z "${OPENHAB_HTTP_PORT}" ]; then OPENHAB_HTTP_PORT=8080; fi
if [ -z "${OPENHAB_HTTPS_PORT}" ]; then OPENHAB_HTTPS_PORT=8443; fi
return 0;
}

# The function has one non-optinal parameter for the application to create a tile for
Expand Down
271 changes: 234 additions & 37 deletions functions/packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -406,62 +406,259 @@ speedtest_cli_setup() {
fi
}

## Function for installing and configure InfluxDB and Grafana while also integrate it to openHAB.
## The function can be invoked either INTERACTIVE with userinterface UNATTENDED.
##
## When called UNATTENDED it will install both InfluxDB and Grafana on the local system.
##
## influxdb_grafana_setup()
##

influxdb_grafana_setup() {
FAILED=0
introtext="This will install InfluxDB and Grafana. Soon this procedure will also set up the connection between them and with openHAB. For now, please follow the instructions found here:
local FAILED=0
local text_intro="This will install and configure InfluxDB and Grafana. Discussion thread about this configuration please consult:
\nhttps://community.openhab.org/t/13761/1"
failtext="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue."
successtext="Setup successful. Please continue with the instructions you can find here:\n\nhttps://community.openhab.org/t/13761/1"
local text_fail="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue."
local text_success="Setup successful. Please continue with the instructions you can find here:\n\nhttps://community.openhab.org/t/13761/1"

echo "$(timestamp) [openHABian] Setting up InfluxDB and Grafana... "
distributor=$(lsb_release -si | awk '{print tolower($0)}')
if ! [ $distributor = "ubuntu" ]; then #since we only have ubuntu and debian as supported OS at the moment
distributor="debian" # replacing e.g. raspbian with debian
fi
codename=$(lsb_release -sc)
if [ -n "$INTERACTIVE" ]; then
if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80) then echo "CANCELED"; return 0; fi
fi
if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$text_intro" 15 80) then echo "CANCELED"; return 0; fi
fi

local influxdb_address
local influxdb_admin_username
local influxdb_admin_password
local influxdb_openhab_username
local influxdb_openhab_password
local influxdb_grafana_username
local influxdb_grafana_password
local influxdb_database_name
local grafana_admin_password
local openhab_integration=false
if [ -n "$INTERACTIVE" ]; then
local text_influxDB_intro="A new InfluxDB instance can be installed locally on the openhabian system or an already running InfluxDB instance can be used."
if ! (whiptail --title "InfluxDB" --yes-button "Install locally" --no-button "Use existing instance" --yesno "$text_influxDB_intro" 15 80) then
local text_influxDB_configure="Shall a new user and database be configured on the InfluxDB instance automatically or shall existing existing ones be used?"
if ! (whiptail --title "InfluxDB" --yes-button "Configure automatically" --no-button "Use existing database" --yesno "$text_influxDB_configure" 15 80) then
# Existing InfluxDB - Manual configuration
influxdb_database_name=$(whiptail --title "InfluxDB" --inputbox "OpenHAB need to use a specific InfluxDB database. Please enter a configured InfluxDB database name:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_openhab_username=$(whiptail --title "InfluxDB" --inputbox "OpenHAB need write/read access to previous defined database. Please enter an InfluxDB username for OpenHAB:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_openhab_password=$(whiptail --title "InfluxDB" --passwordbox "Password for InfluxDB account \"$influxdb_openhab_username:\"" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_grafana_username=$(whiptail --title "InfluxDB" --inputbox "Grafana need read access to previous defined database. Please enter an InfluxDB username for Grafana:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_grafana_password=$(whiptail --title "InfluxDB" --passwordbox "Password for InfluxDB account \"$influxdb_grafana_username\":" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
else
# Existing InfluxBD - Automatic configuration
influxdb_admin_username=$(whiptail --title "InfluxDB" --inputbox "An InfluxDB admin account must be used for automatical database configuration. Please enter a username: " 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_admin_password=$(whiptail --title "InfluxDB" --passwordbox "Password for InfluxDB account \"$influxdb_admin_username\":" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
fi
# Influx DB server address
local influxdb_returncode=0
local text_influxdb_address="Enter InfluxDB instance adress: [protocol:address:port] \n eg. https://192.168.1.100:8086"
while [ "$influxdb_returncode" != "204" ]
do
influxdb_address=$(whiptail --title "InfluxDB" --inputbox "$text_influxdb_address" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_returncode=`curl -s -o --max-time 6 --insecure /dev/null -w "%{http_code}" "$influxdb_address"/ping | sed 's/^0*//'`
text_influxdb_address="Can't establish contact to InfluxDB instance. Please retry to enter InfluxDB instance adress: [protocol:address:port] \n eg. https://192.168.1.100:8086"
done
else
# Local InfluxDB
local text_influxDB_admin_password="The local InfluxDB installation needs a password for the \"admin\" account. Enter a password:"
local matched=false
local password_check
while [ "$matched" = false ]; do
influxdb_admin_password=$(whiptail --title "InfluxDB - Admin Account" --passwordbox "$text_influxDB_admin_password" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
password_check=$(whiptail --title "InfluxDB - Admin Account" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
if [ "$influxdb_admin_password" = "$password_check" ] && [ ! -z "$influxdb_admin_password" ]; then
matched=true
else
$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3)
fi
done
fi

if [ -z "$influxdb_openhab_username" ]; then # is empty
influxdb_database_name="openhab_db"
influxdb_openhab_username="openhab"
influxdb_grafana_username="grafana"
local matched=false
local password_check
while [ "$matched" = false ]; do
influxdb_openhab_password=$(whiptail --title "InfluxDB - OpenHAB Account" --passwordbox "An openHAB specific InfluxDB user will be created \"openhab\". Please enter a password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
password_check=$(whiptail --title "InfluxDB - OpenHAB Account" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
if [ "$influxdb_openhab_password" = "$password_check" ] && [ ! -z "$influxdb_openhab_password" ]; then
matched=true
else
$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3)
fi
done
matched=false
while [ "$matched" = false ]; do
influxdb_grafana_password=$(whiptail --title "InfluxDB - Grafana Account" --passwordbox "A Grafana specific InfluxDB user will be created \"grafana\". Please enter a password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
password_check=$(whiptail --title "InfluxDB - Grafana Account" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
if [ "$influxdb_grafana_password" = "$password_check" ] && [ ! -z "$influxdb_grafana_password" ]; then
matched=true
else
$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3)
fi
done
fi

# Local Grafana
local text_grafana_admin_password="The local Grafana installation needs a password for the \"admin\" account. Enter a password:"
local matched=false
local password_check
while [ "$matched" = false ]; do
grafana_admin_password=$(whiptail --title "Grafana - Admin Account" --passwordbox "$text_grafana_admin_password" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
password_check=$(whiptail --title "Grafana - Admin Account" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
if [ "$grafana_admin_password" = "$password_check" ] && [ ! -z "$grafana_admin_password" ]; then
matched=true
else
$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3)
fi
done

cond_redirect apt -y install apt-transport-https
cond_echo ""
echo "InfluxDB... "
cond_redirect wget -O - https://repos.influxdata.com/influxdb.key | apt-key add - || FAILED=1
echo "deb https://repos.influxdata.com/$distributor $codename stable" > /etc/apt/sources.list.d/influxdb.list || FAILED=1
cond_redirect apt update || FAILED=1
cond_redirect apt -y install influxdb || FAILED=1
cond_redirect systemctl daemon-reload
cond_redirect systemctl enable influxdb.service
cond_redirect systemctl start influxdb.service
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
cond_echo ""

echo "Grafana... "
if is_pi; then
if is_pione || is_pizero || is_pizerow; then GRAFANA_REPO_PI1="-rpi-1b"; fi
echo "deb https://dl.bintray.com/fg2it/deb${GRAFANA_REPO_PI1} $codename main" > /etc/apt/sources.list.d/grafana-fg2it.list || FAILED=2
cond_redirect apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 379CE192D401AB61 || FAILED=2
if openhab_is_running; then
local text_openHAB_integration="OpenHAB can use InfluxDB for persistant storage. Shall InfluxDB be configured with OpenHAB?
(A new config file for openHAB will be created with basic settings.)"
if (whiptail --title "OpenHAB integration, Continue?" --yes-button "Yes" --no-button "No" --yesno "$text_openHAB_integration" 15 80) then openhab_integration=true ; fi
else
cond_echo "OpenHAB is not running. InfluxDB and grafana openHAB integration is skipped..."
fi
fi

local dist="debian"
local codename="stretch"
if is_ubuntu; then
dist="ubuntu"
codename=$(lsb_release -sc)
elif is_debian; then
dist="debian"
codename=$(lsb_release -sc)
fi

if [ -z "$influxdb_address" ]; then # is empty, install a InfluxDB database
cond_echo ""
echo "Installing InfluxDB..."
influxdb_address="http://localhost:8086"
influxdb_admin_username="admin"
if [ ! -f /etc/influxdb/influxdb.conf ]; then
cond_redirect apt -y install apt-transport-https
cond_redirect wget -O - https://repos.influxdata.com/influxdb.key | apt-key add - || FAILED=1
echo "deb https://repos.influxdata.com/$dist $codename stable" > /etc/apt/sources.list.d/influxdb.list || FAILED=1
cond_redirect apt update || FAILED=1
cond_redirect apt -y install influxdb || FAILED=1
cond_redirect systemctl daemon-reload
sleep 2
cond_redirect systemctl enable influxdb.service
sleep 2
cond_redirect systemctl restart influxdb.service
sleep 10
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
echo -n "Configure InfluxDB admin account... "; echo -n ""
curl --insecure $influxdb_address/query --data-urlencode "q=CREATE USER admin WITH PASSWORD '$influxdb_admin_password' WITH ALL PRIVILEGES" || FAILED=1
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
echo -n "Configure listen on localhost only... "; echo -n ""
cond_redirect sed -i -e '/^# Determines whether HTTP endpoint is enabled./ { n ; s/^# enabled = true/enabled = true/ }' /etc/influxdb/influxdb.conf
cond_redirect sed -i 's/# bind-address = ":8086"/bind-address = "localhost:8086"/g' /etc/influxdb/influxdb.conf
cond_redirect sed -i 's/# auth-enabled = false/auth-enabled = true/g' /etc/influxdb/influxdb.conf
cond_redirect systemctl restart influxdb.service
sleep 10
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
else
echo "SKIPPED"
cond_echo "InfluxDB already installed. Using http://localhost:8086"
fi
cond_echo ""
fi

if [ ! -z "$influxdb_admin_username" ]; then # is set, configure database and application users
echo -n "Setup of inital influxdb database and InfluxDB users... "
echo -n ""
influxdb_database_name="openhab_db"
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=CREATE DATABASE $influxdb_database_name" || FAILED=1
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=CREATE USER openhab WITH PASSWORD '$influxdb_openhab_password'" || FAILED=1
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=CREATE USER grafana WITH PASSWORD '$influxdb_grafana_password'" || FAILED=1
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=GRANT ALL ON openhab_db TO openhab" || FAILED=1
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=GRANT READ ON openhab_db TO grafana" || FAILED=1
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
fi

echo -n "Installing Grafana..."
# TODO: Update with ARM repository when ready; https://github.com/grafana/grafana/issues/12761
local architecture=$(dpkg --print-architecture)
if architecture="armhf"; then
cond_redirect wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.3.4_armhf.deb || FAILED=2
cond_redirect apt update || FAILED=2
cond_redirect apt-get install -y adduser libfontconfig
cond_redirect dpkg -i grafana_5.3.4_armhf.deb
elif architecture="arm64" then
cond_redirect wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.3.4_arm64.deb || FAILED=2
cond_redirect apt update || FAILED=2
cond_redirect apt-get install -y adduser libfontconfig
cond_redirect dpkg -i grafana_5.3.4_arm64.deb
else
cond_redirect wget -O - https://packagecloud.io/gpg.key | apt-key add - || FAILED=2
echo "deb https://packagecloud.io/grafana/stable/$distributor/ $codename main" > /etc/apt/sources.list.d/grafana.list || FAILED=2
if [ $distributor = "ubuntu" ]; then #remove this loop when grafana ubuntu packages are available
echo "deb https://packagecloud.io/grafana/stable/debian/ jessie main" > /etc/apt/sources.list.d/grafana.list || FAILED=2
echo "deb https://packagecloud.io/grafana/stable/$dist/ $codename main" > /etc/apt/sources.list.d/grafana.list || FAILED=2
if [ $dist = "ubuntu" ]; then #remove this loop when grafana ubuntu packages are available
echo "deb https://packagecloud.io/grafana/stable/debian/ stretch main" > /etc/apt/sources.list.d/grafana.list || FAILED=2
fi
cond_redirect apt update || FAILED=2
cond_redirect apt -y install grafana || FAILED=2
fi
cond_redirect apt update || FAILED=2
cond_redirect apt -y install grafana || FAILED=2
cond_redirect systemctl daemon-reload
cond_redirect systemctl enable grafana-server.service
cond_redirect systemctl start grafana-server.service
sleep 10
if [ $FAILED -eq 2 ]; then echo -n "FAILED "; else echo -n "OK "; fi
cond_echo ""

echo "Connecting (TODO)... "
#TODO
echo -n "Updating Grafana admin password..."
curl --user admin:admin --header "Content-Type: application/json" --request PUT --data "{\"password\":\"$grafana_admin_password\"}" http://localhost:3000/api/admin/users/1/password || FAILED=2
if [ $FAILED -eq 2 ]; then echo -n "FAILED "; else echo -n "OK "; fi

echo "Adding openHAB dashboard tile... "
echo -n "Updating Grafan configuration..."
cond_redirect sed -i -e '/^# disable user signup \/ registration/ { n ; s/^;allow_sign_up = true/allow_sign_up = false/ }' /etc/grafana/grafana.ini
cond_redirect sed -i -e '/^# enable anonymous access/ { n ; s/^;enabled = false/enabled = true/ }' /etc/grafana/grafana.ini
cond_redirect systemctl restart grafana-server.service
sleep 10

echo -n "Connection Grafana to InfluxDB..."
curl --user admin:$grafana_admin_password --request POST http://localhost:3000/api/datasources \
--header "Content-Type: application/json" \
--data '{"name": "openhab_home", "type": "influxdb", "url": "http://localhost:8086", "password": "'"$influxdb_grafana_password"'", "user": "'"$influxdb_grafana_user"'", "database": "'"$influxdb_database_name"'", "access": "proxy", "basicAuth":true, "basicAuthUser":"'"$influxdb_grafana_user"'", "basicAuthPassword":"'"$influxdb_grafana_password"'", "withCredentials":false}'

echo -n "Adding openHAB dashboard tile for Grafana... "
dashboard_add_tile grafana || FAILED=4

if [ "$openhab_integration" = true ]; then
echo -n "Adding install InfluxDB with database configuration to OpenHAB"
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" "http://localhost:$OPENHAB_HTTP_PORT/rest/extensions/influxdb/install"
cond_redirect touch /etc/openhab2/services/influxdb.cfg
echo "url=$influxdb_address" >> /etc/openhab2/services/influxdb.cfg
echo "user=$influxdb_openhab_username" >> /etc/openhab2/services/influxdb.cfg
echo "password=$influxdb_openhab_password" >> /etc/openhab2/services/influxdb.cfg
echo "db=$influxdb_database_name" >> /etc/openhab2/services/influxdb.cfg
echo "retentionPolicy=autogen" >> /etc/openhab2/services/influxdb.cfg
fi

if [ -n "$INTERACTIVE" ]; then
if [ $FAILED -eq 0 ]; then
whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80
Expand Down Expand Up @@ -700,4 +897,4 @@ or just reboot the system.
whiptail --title "Operation Failed!" --msgbox "$failtext" 10 60
fi
fi
}
}