streampipes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wie...@apache.org
Subject [incubator-streampipes-installer] 02/02: extract docker0 bridge IP (linux), docker for mac and windows vm IP automatically
Date Fri, 13 Dec 2019 14:31:31 GMT
This is an automated email from the ASF dual-hosted git repository.

wiener pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes-installer.git

commit 953322165aa109b66172da06c060b166524c3678
Author: Patrick Wiener <wiener@fzi.de>
AuthorDate: Fri Dec 13 15:27:33 2019 +0100

    extract docker0 bridge IP (linux), docker for mac and windows vm IP automatically
---
 cli/sp    | 187 +++++++++++---------------------------------------------------
 cli/sp.m4 | 155 ++++++++++-----------------------------------------
 2 files changed, 60 insertions(+), 282 deletions(-)

diff --git a/cli/sp b/cli/sp
index 8b36659..35bc9f6 100755
--- a/cli/sp
+++ b/cli/sp
@@ -15,11 +15,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# ARG_OPTIONAL_SINGLE([hostname],[],[Set the default hostname of your server by providing
the IP or DNS name],[])
-# ARG_OPTIONAL_BOOLEAN([defaultip],[d],[When set the first ip is used as default])
+#!/bin/bash
+
 # ARG_OPTIONAL_BOOLEAN([all],[a],[Select all available StreamPipes services])
 # ARG_POSITIONAL_MULTI([operation],[The StreamPipes operation (operation-name) (service-name
(optional))],[3],[],[])
-# ARG_TYPE_GROUP_SET([operation],[type string],[operation],[start,stop,restart,update,set-template,logs,list-available,list-active,list-templates,activate,add,deactivate,clean,force-clean,remove-settings,set-env,unset-env,create-compose,set-version])
+# ARG_TYPE_GROUP_SET([operation],[type string],[operation],[start,stop,restart,update,set-template,logs,list-available,list-active,list-templates,activate,add,deactivate,clean,force-clean,remove-settings,set-version])
 # ARG_DEFAULTS_POS()
 # ARG_HELP([This script provides advanced features to run StreamPipes on your server])
 # ARG_VERSION([echo This is the StreamPipes dev installer v0.1])
@@ -44,18 +44,18 @@ die()
 
 operation()
 {
-	local _allowed=("start" "stop" "restart" "update" "set-template" "logs" "list-available"
"list-active" "list-templates" "activate" "add" "deactivate" "clean" "force-clean" "remove-settings"
"set-env" "unset-env" "create-compose" "set-version") _seeking="$1"
+	local _allowed=("start" "stop" "restart" "update" "set-template" "logs" "list-available"
"list-active" "list-templates" "activate" "add" "deactivate" "clean" "force-clean" "remove-settings"
"set-version") _seeking="$1"
 	for element in "${_allowed[@]}"
 	do
 		test "$element" = "$_seeking" && echo "$element" && return 0
 	done
-	die "Value '$_seeking' (of argument '$2') doesn't match the list of allowed values: 'start',
'stop', 'restart', 'update', 'set-template', 'logs', 'list-available', 'list-active', 'list-templates',
'activate', 'add', 'deactivate', 'clean', 'force-clean', 'remove-settings', 'set-env', 'unset-env',
'create-compose' and 'set-version'" 4
+	die "Value '$_seeking' (of argument '$2') doesn't match the list of allowed values: 'start',
'stop', 'restart', 'update', 'set-template', 'logs', 'list-available', 'list-active', 'list-templates',
'activate', 'add', 'deactivate', 'clean', 'force-clean', 'remove-settings' and 'set-version'"
4
 }
 
 
 begins_with_short_option()
 {
-  local first_option all_short_options='dahv'
+  local first_option all_short_options='ahv'
   first_option="${1:0:1}"
   test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 ||
return 0
 }
@@ -64,18 +64,14 @@ begins_with_short_option()
 _positionals=()
 _arg_operation=('' )
 # THE DEFAULTS INITIALIZATION - OPTIONALS
-_arg_hostname=
-_arg_defaultip="off"
 _arg_all="off"
 
 
 print_help()
 {
   printf '%s\n' "This script provides advanced features to run StreamPipes on your server"
-  printf 'Usage: %s [--hostname <arg>] [-d|--(no-)defaultip] [-a|--(no-)all] [-h|--help]
[-v|--version] <operation-1> [<operation-2>] [<operation-3>]\n' "$0"
+  printf 'Usage: %s [-a|--(no-)all] [-h|--help] [-v|--version] <operation-1> [<operation-2>]
[<operation-3>]\n' "$0"
   printf '\t%s\n' "<operation>: The StreamPipes operation (operation-name) (service-name
(optional)) (defaults for <operation-2> to <operation-3> respectively: '' and
'')"
-  printf '\t%s\n' "--hostname: Set the default hostname of your server by providing the IP
or DNS name (no default)"
-  printf '\t%s\n' "-d, --defaultip, --no-defaultip: When set the first ip is used as default
(off by default)"
   printf '\t%s\n' "-a, --all, --no-all: Select all available StreamPipes services (off by
default)"
   printf '\t%s\n' "-h, --help: Prints help"
   printf '\t%s\n' "-v, --version: Prints version"
@@ -89,26 +85,6 @@ parse_commandline()
   do
     _key="$1"
     case "$_key" in
-      --hostname)
-        test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
-        _arg_hostname="$2"
-        shift
-        ;;
-      --hostname=*)
-        _arg_hostname="${_key##--hostname=}"
-        ;;
-      -d|--no-defaultip|--defaultip)
-        _arg_defaultip="on"
-        test "${1:0:5}" = "--no-" && _arg_defaultip="off"
-        ;;
-      -d*)
-        _arg_defaultip="on"
-        _next="${_key##-d}"
-        if test -n "$_next" -a "$_next" != "$_key"
-        then
-          begins_with_short_option "$_next" && shift && set -- "-d" "-${_next}"
"$@" || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because
${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
-        fi
-        ;;
       -a|--no-all|--all)
         _arg_all="on"
         test "${1:0:5}" = "--no-" && _arg_all="off"
@@ -204,10 +180,10 @@ fatal()
 deployment_notice() {
 	echo
 	echo
-	echo "  INFO: StreamPipes CE $1 is now ready to be used on your system"
-	echo "        Check https://streampipes.org/ for information on StreamPipes"
+	echo "INFO: StreamPipes CE $1 is now ready to be used on your system"
+	echo "      Check https://streampipes.org/ for information on StreamPipes"
 	echo
-	echo "        Go to the UI and follow the instructions to get started: http://$2/"
+	echo "      Go to the UI and follow the instructions to get started: http://$2/"
 	echo
 }
 
@@ -218,113 +194,24 @@ if [ $? -ne 0 ]; then
     fatal "Error occured while executing the StreamPipes command."
 fi
 
-#	if [ $_arg_logs = "on" ];
-#	then
-
-#	else
-#		$1 > /dev/null 2>&1
-#	fi
-}
-
-getIp() {
-    if [ -x "$(command -v ifconfig)" ]; then
-        rawip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'
| grep -v '127.0.0.1')
-    elif [ -x "$(command -v ipconfig)" ]; then
-        rawip=$(ipconfig | grep -Eo 'IPv4.*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
-    fi
-
-    rawip=`echo $rawip | sed 's/(%s)*\n/ /g'`
-    IFS=' ' declare -a 'allips=($rawip)'
-
-    allips+=( 'Enter IP manually' )
-
-    # if default selected do not show prompt
-
-    if [ $_arg_hostname ] ;
-    then
-        ip=$_arg_hostname
-        info 'Default IP was selected: '${ip}
-    else
-
-				if [ $_arg_defaultip = "on" ];
-				then
-        	ip=${allips[0]}
-        	info 'Default IP was selected: '${ip}
-				else
-					echo ''
-					info 'Please select your IP address or add one manually: '
-					PS3='Select option: '
-					select opt in "${allips[@]}"
-					do
-							if [ -z "${opt}" ];
-							then
-									warning "Wrong input, select one of the options";
-							else
-									ip="$opt"
-
-									if [ "$opt" == "Enter IP manually" ];
-									then
-											read -p "Enter Ip: " ip
-									fi
-									break
-							fi
-        	done
-				fi
-    fi
-
 }
 
-#TODO: adapt to new format
-# createCompose() {
-#   IFS=''
-# 	# Get consul
-# 	#result=$(head -n 28 docker-compose.yml)
-#
-# 	while read service; do
-#   	result=$result"\n \n"$(tail -n +3 "services/$service/docker-compose.yml" | sed '/spnet:/q')
-# 	done <system
-#
-# 	# Generate network
-# 	#result=$result"\n \n"$(tail -n -13 docker-compose.yml)
-#
-# 	echo -e "$result" > "docker-compose.yml-generated"
-#
-# 	info "New compose file is generated: docker-compose.tml-generated"
-# }
-
-unsetEnv() {
-	cd services
-	for dir in */ ; do
-  file="$dir"docker-compose.yml
-
-    one=${_arg_operation[1]}"="
-	  two=${_arg_operation[2]}
-
-	  result="$one$two"
-
-	  IFS=''
-
-    while read a ; do echo ${a//      - $one*/#      - $one} ; done < $file > ./$file.t
; mv $file{.t,}
-	done
-	cd ..
-}
-
-setEnv() {
-	cd services
-	for dir in */ ; do
-  	file="$dir"docker-compose.yml
-
-    one=${_arg_operation[1]}"="
-	  two=${_arg_operation[2]}
-
-	  result="$one$two"
-
-	  IFS=''
-
-	  while read a ; do echo ${a//#      - $one/      - $result} ; done < $file > ./$file.t
; mv $file{.t,}
-
-	done
-	cd ..
+getHostDockerInteralIP() {
+  # get os type
+  OS_TYPE=`uname`
+  info "Detected OS: ${OS_TYPE}"
+  if [ "$OS_TYPE" == "Darwin" ] || [ "$OS_TYPE" == "CYGWIN" ]; then
+    # get IP of docker vm hosting docker for mac and docker windows
+    ip=$(docker run --rm -it alpine getent hosts host.docker.internal | awk '{ print $1 }')
> /dev/null 2>&1
+    docker rmi alpine > /dev/null 2>&1
+    info "Detected Docker Host IP: ${ip}"
+    # get IP of docker0 bridge
+  elif [ "$OS_TYPE" == "Linux" ]; then
+    ip=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
+    info "Detected Docker0 bridge IP: ${ip}"
+  else
+    fatal "Detected OS not supporeted: ${OS_TYPE}"
+  fi
 }
 
 moveSystemConfig() {
@@ -356,7 +243,7 @@ setVersion() {
 	sed "s/SP_BACKEND_VERSION=.*/${version}/g" ./tmpl_env > ./del_tmpl_env
 	mv ./del_tmpl_env ./tmpl_env
 
-	echo "Change StreamPipes version to ${_arg_operation[1]}"
+	info "Change StreamPipes version to ${_arg_operation[1]}"
 }
 
 getCommand() {
@@ -373,10 +260,11 @@ startStreamPipes() {
 		moveSystemConfig default
 	fi
 
-	if [ ! -f "$SP_HOME/.env" ] || [ $_arg_defaultip = "on" ];
+	if [ ! -f "$SP_HOME/.env" ];
     then
-		getIp
-		sed "s/##IP##/${ip}/g" $SP_HOME/tmpl_env > $SP_HOME/.env
+    info 'Initial StreamPipes configuration'
+    getHostDockerInteralIP
+    sed "s/##HOST_DOCKER_INTERNAL##/${ip}/g" $SP_HOME/tmpl_env > $SP_HOME/.env
 	fi
     info "Starting StreamPipes ${_arg_operation[1]}"
 		createNetwork
@@ -384,10 +272,8 @@ startStreamPipes() {
     run "$command up -d ${_arg_operation[1]}"
 
 		SP_BACKEND_VERSION=`grep SP_BACKEND_VERSION "$SP_HOME/.env" | awk -F= '{print $2}'`
-		SP_HOST=`grep SP_HOST "$SP_HOME/.env" | awk -F= '{print $2}'`
 
-    #info "StreamPipes started ${_arg_operation[1]}"
-		deployment_notice $SP_BACKEND_VERSION $SP_HOST
+		deployment_notice $SP_BACKEND_VERSION "localhost"
 }
 
 updateStreamPipes() {
@@ -636,15 +522,6 @@ then
     setTemplate
 fi
 
-# if [ "$_arg_operation" = "set-env" ];
-# then
-#     setEnv
-# fi
-
-# if [ "$_arg_operation" = "create-compose" ];
-# then
-#    createCompose
-# fi
 
 if [ "$_arg_operation" = "set-version" ];
 then
diff --git a/cli/sp.m4 b/cli/sp.m4
index 4a20bf1..022609d 100644
--- a/cli/sp.m4
+++ b/cli/sp.m4
@@ -15,11 +15,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# ARG_OPTIONAL_SINGLE([hostname],, [Set the default hostname of your server by providing
the IP or DNS name], )
-# ARG_OPTIONAL_BOOLEAN([defaultip],d, [When set the first ip is used as default])
+#!/bin/bash
+
 # ARG_OPTIONAL_BOOLEAN([all],a, [Select all available StreamPipes services])
 # ARG_POSITIONAL_MULTI([operation], [The StreamPipes operation (operation-name) (service-name
(optional))], 3, [], [])
-# ARG_TYPE_GROUP_SET([operation], [type string], [operation], [start,stop,restart,update,set-template,logs,list-available,list-active,list-templates,activate,add,deactivate,clean,force-clean,remove-settings,set-env,unset-env,create-compose,set-version])
+# ARG_TYPE_GROUP_SET([operation], [type string], [operation], [start,stop,restart,update,set-template,logs,list-available,list-active,list-templates,activate,add,deactivate,clean,force-clean,remove-settings,set-version])
 # ARG_DEFAULTS_POS
 # ARG_HELP([This script provides advanced features to run StreamPipes on your server])
 # ARG_VERSION([echo This is the StreamPipes dev installer v0.1])
@@ -50,10 +50,10 @@ fatal()
 deployment_notice() {
 	echo
 	echo
-	echo "  INFO: StreamPipes CE $1 is now ready to be used on your system"
-	echo "        Check https://streampipes.org/ for information on StreamPipes"
+	echo "INFO: StreamPipes CE $1 is now ready to be used on your system"
+	echo "      Check https://streampipes.org/ for information on StreamPipes"
 	echo
-	echo "        Go to the UI and follow the instructions to get started: http://$2/"
+	echo "      Go to the UI and follow the instructions to get started: http://$2/"
 	echo
 }
 
@@ -64,113 +64,24 @@ if [ $? -ne 0 ]; then
     fatal "Error occured while executing the StreamPipes command."
 fi
 
-#	if [ $_arg_logs = "on" ];
-#	then
-
-#	else
-#		$1 > /dev/null 2>&1
-#	fi
-}
-
-getIp() {
-    if [ -x "$(command -v ifconfig)" ]; then
-        rawip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'
| grep -v '127.0.0.1')
-    elif [ -x "$(command -v ipconfig)" ]; then
-        rawip=$(ipconfig | grep -Eo 'IPv4.*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
-    fi
-
-    rawip=`echo $rawip | sed 's/(%s)*\n/ /g'`
-    IFS=' ' declare -a 'allips=($rawip)'
-
-    allips+=( 'Enter IP manually' )
-
-    # if default selected do not show prompt
-
-    if [ $_arg_hostname ] ;
-    then
-        ip=$_arg_hostname
-        info 'Default IP was selected: '${ip}
-    else
-
-				if [ $_arg_defaultip = "on" ];
-				then
-        	ip=${allips[0]}
-        	info 'Default IP was selected: '${ip}
-				else
-					echo ''
-					info 'Please select your IP address or add one manually: '
-					PS3='Select option: '
-					select opt in "${allips[@]}"
-					do
-							if [ -z "${opt}" ];
-							then
-									warning "Wrong input, select one of the options";
-							else
-									ip="$opt"
-
-									if [ "$opt" == "Enter IP manually" ];
-									then
-											read -p "Enter Ip: " ip
-									fi
-									break
-							fi
-        	done
-				fi
-    fi
-
-}
-
-#TODO: adapt to new format
-# createCompose() {
-#   IFS=''
-# 	# Get consul
-# 	#result=$(head -n 28 docker-compose.yml)
-#
-# 	while read service; do
-#   	result=$result"\n \n"$(tail -n +3 "services/$service/docker-compose.yml" | sed '/spnet:/q')
-# 	done <system
-#
-# 	# Generate network
-# 	#result=$result"\n \n"$(tail -n -13 docker-compose.yml)
-#
-# 	echo -e "$result" > "docker-compose.yml-generated"
-#
-# 	info "New compose file is generated: docker-compose.tml-generated"
-# }
-
-unsetEnv() {
-	cd services
-	for dir in */ ; do
-  file="$dir"docker-compose.yml
-
-    one=${_arg_operation[1]}"="
-	  two=${_arg_operation[2]}
-
-	  result="$one$two"
-
-	  IFS=''
-
-    while read a ; do echo ${a//      - $one*/#      - $one} ; done < $file > ./$file.t
; mv $file{.t,}
-	done
-	cd ..
 }
 
-setEnv() {
-	cd services
-	for dir in */ ; do
-  	file="$dir"docker-compose.yml
-
-    one=${_arg_operation[1]}"="
-	  two=${_arg_operation[2]}
-
-	  result="$one$two"
-
-	  IFS=''
-
-	  while read a ; do echo ${a//#      - $one/      - $result} ; done < $file > ./$file.t
; mv $file{.t,}
-
-	done
-	cd ..
+getHostDockerInteralIP() {
+  # get os type
+  OS_TYPE=`uname`
+  info "Detected OS: ${OS_TYPE}"
+  if [ "$OS_TYPE" == "Darwin" ] || [ "$OS_TYPE" == "CYGWIN" ]; then
+    # get IP of docker vm hosting docker for mac and docker windows
+    ip=$(docker run --rm -it alpine getent hosts host.docker.internal | awk '{ print $1 }')
> /dev/null 2>&1
+    docker rmi alpine > /dev/null 2>&1
+    info "Detected Docker Host IP: ${ip}"
+    # get IP of docker0 bridge
+  elif [ "$OS_TYPE" == "Linux" ]; then
+    ip=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
+    info "Detected Docker0 bridge IP: ${ip}"
+  else
+    fatal "Detected OS not supporeted: ${OS_TYPE}"
+  fi
 }
 
 moveSystemConfig() {
@@ -202,7 +113,7 @@ setVersion() {
 	sed "s/SP_BACKEND_VERSION=.*/${version}/g" ./tmpl_env > ./del_tmpl_env
 	mv ./del_tmpl_env ./tmpl_env
 
-	echo "Change StreamPipes version to ${_arg_operation[1]}"
+	info "Change StreamPipes version to ${_arg_operation[1]}"
 }
 
 getCommand() {
@@ -219,10 +130,11 @@ startStreamPipes() {
 		moveSystemConfig default
 	fi
 
-	if [ ! -f "$SP_HOME/.env" ] || [ $_arg_defaultip = "on" ];
+	if [ ! -f "$SP_HOME/.env" ];
     then
-		getIp
-		sed "s/##IP##/${ip}/g" $SP_HOME/tmpl_env > $SP_HOME/.env
+    info 'Initial StreamPipes configuration'
+    getHostDockerInteralIP
+    sed "s/##HOST_DOCKER_INTERNAL##/${ip}/g" $SP_HOME/tmpl_env > $SP_HOME/.env
 	fi
     info "Starting StreamPipes ${_arg_operation[1]}"
 		createNetwork
@@ -230,10 +142,8 @@ startStreamPipes() {
     run "$command up -d ${_arg_operation[1]}"
 
 		SP_BACKEND_VERSION=`grep SP_BACKEND_VERSION "$SP_HOME/.env" | awk -F= '{print $2}'`
-		SP_HOST=`grep SP_HOST "$SP_HOME/.env" | awk -F= '{print $2}'`
 
-    #info "StreamPipes started ${_arg_operation[1]}"
-		deployment_notice $SP_BACKEND_VERSION $SP_HOST
+		deployment_notice $SP_BACKEND_VERSION "localhost"
 }
 
 updateStreamPipes() {
@@ -482,15 +392,6 @@ then
     setTemplate
 fi
 
-# if [ "$_arg_operation" = "set-env" ];
-# then
-#     setEnv
-# fi
-
-# if [ "$_arg_operation" = "create-compose" ];
-# then
-#    createCompose
-# fi
 
 if [ "$_arg_operation" = "set-version" ];
 then


Mime
View raw message