浏览代码

Merge pull request #6424 from rglidden/truenas_ws_remote

truenas_ws: Add ability to deploy to remote TrueNAS server via WebSockets
neil 1 月之前
父节点
当前提交
ca7bdd9101
共有 1 个文件被更改,包括 24 次插入6 次删除
  1. 24 6
      deploy/truenas_ws.sh

+ 24 - 6
deploy/truenas_ws.sh

@@ -39,13 +39,13 @@ _ws_call() {
   _debug "_ws_call arg2" "$2"
   _debug "_ws_call arg2" "$2"
   _debug "_ws_call arg3" "$3"
   _debug "_ws_call arg3" "$3"
   if [ $# -eq 3 ]; then
   if [ $# -eq 3 ]; then
-    _ws_response=$(midclt -K "$DEPLOY_TRUENAS_APIKEY" call "$1" "$2" "$3")
+    _ws_response=$(midclt --uri "$_ws_uri" -K "$DEPLOY_TRUENAS_APIKEY" call "$1" "$2" "$3")
   fi
   fi
   if [ $# -eq 2 ]; then
   if [ $# -eq 2 ]; then
-    _ws_response=$(midclt -K "$DEPLOY_TRUENAS_APIKEY" call "$1" "$2")
+    _ws_response=$(midclt --uri "$_ws_uri" -K "$DEPLOY_TRUENAS_APIKEY" call "$1" "$2")
   fi
   fi
   if [ $# -eq 1 ]; then
   if [ $# -eq 1 ]; then
-    _ws_response=$(midclt -K "$DEPLOY_TRUENAS_APIKEY" call "$1")
+    _ws_response=$(midclt --uri "$_ws_uri" -K "$DEPLOY_TRUENAS_APIKEY" call "$1")
   fi
   fi
   _debug "_ws_response" "$_ws_response"
   _debug "_ws_response" "$_ws_response"
   printf "%s" "$_ws_response"
   printf "%s" "$_ws_response"
@@ -60,7 +60,7 @@ _ws_upload_cert() {
 import sys
 import sys
 
 
 from truenas_api_client import Client
 from truenas_api_client import Client
-with Client() as c:
+with Client(uri="$_ws_uri") as c:
 
 
   ### Login with API key
   ### Login with API key
   print("I:Trying to upload new certificate...")
   print("I:Trying to upload new certificate...")
@@ -121,7 +121,7 @@ _ws_check_jobid() {
 #   n/a
 #   n/a
 _ws_get_job_result() {
 _ws_get_job_result() {
   while true; do
   while true; do
-    sleep 2
+    _sleep 2
     _ws_response=$(_ws_call "core.get_jobs" "[[\"id\", \"=\", $1]]")
     _ws_response=$(_ws_call "core.get_jobs" "[[\"id\", \"=\", $1]]")
     if [ "$(printf "%s" "$_ws_response" | jq -r '.[]."state"')" != "RUNNING" ]; then
     if [ "$(printf "%s" "$_ws_response" | jq -r '.[]."state"')" != "RUNNING" ]; then
       _ws_result="$(printf "%s" "$_ws_response" | jq '.[]."result"')"
       _ws_result="$(printf "%s" "$_ws_response" | jq '.[]."result"')"
@@ -179,11 +179,27 @@ truenas_ws_deploy() {
 
 
   _info "Checking environment variables..."
   _info "Checking environment variables..."
   _getdeployconf DEPLOY_TRUENAS_APIKEY
   _getdeployconf DEPLOY_TRUENAS_APIKEY
+  _getdeployconf DEPLOY_TRUENAS_HOSTNAME
+  _getdeployconf DEPLOY_TRUENAS_PROTOCOL
   # Check API Key
   # Check API Key
   if [ -z "$DEPLOY_TRUENAS_APIKEY" ]; then
   if [ -z "$DEPLOY_TRUENAS_APIKEY" ]; then
     _err "TrueNAS API key not found, please set the DEPLOY_TRUENAS_APIKEY environment variable."
     _err "TrueNAS API key not found, please set the DEPLOY_TRUENAS_APIKEY environment variable."
     return 1
     return 1
   fi
   fi
+  # Check Hostname, default to localhost if not set
+  if [ -z "$DEPLOY_TRUENAS_HOSTNAME" ]; then
+    _info "TrueNAS hostname not set. Using 'localhost'."
+    DEPLOY_TRUENAS_HOSTNAME="localhost"
+  fi
+  # Check protocol, default to ws if not set
+  if [ -z "$DEPLOY_TRUENAS_PROTOCOL" ]; then
+    _info "TrueNAS protocol not set. Using 'ws'."
+    DEPLOY_TRUENAS_PROTOCOL="ws"
+  fi
+  _ws_uri="$DEPLOY_TRUENAS_PROTOCOL://$DEPLOY_TRUENAS_HOSTNAME/websocket"
+  _debug2 DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
+  _debug2 DEPLOY_TRUENAS_PROTOCOL "$DEPLOY_TRUENAS_PROTOCOL"
+  _debug _ws_uri "$_ws_uri"
   _secure_debug2 DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
   _secure_debug2 DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
   _info "Environment variables: OK"
   _info "Environment variables: OK"
 
 
@@ -205,6 +221,8 @@ truenas_ws_deploy() {
     return 2
     return 2
   fi
   fi
   _savedeployconf DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
   _savedeployconf DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
+  _savedeployconf DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
+  _savedeployconf DEPLOY_TRUENAS_PROTOCOL "$DEPLOY_TRUENAS_PROTOCOL"
   _info "TrueNAS health: OK"
   _info "TrueNAS health: OK"
 
 
   ########## System info
   ########## System info
@@ -304,7 +322,7 @@ truenas_ws_deploy() {
   _info "Restarting WebUI..."
   _info "Restarting WebUI..."
   _ws_response=$(_ws_call "system.general.ui_restart")
   _ws_response=$(_ws_call "system.general.ui_restart")
   _info "Waiting for UI restart..."
   _info "Waiting for UI restart..."
-  sleep 6
+  _sleep 15
 
 
   ########## Certificates
   ########## Certificates