upgraded install script, now supports cli arguments to dynamically adjust various settings, eg. domain, primary port, subdomains, names, etc.

This commit is contained in:
Joshua Ashton
2024-11-01 14:50:14 -06:00
committed by Violet Ash
parent 37fecde0c8
commit cdac0e0ec8
5 changed files with 118 additions and 9 deletions
-3
View File
@@ -1,3 +0,0 @@
#!/bin/zsh
# Install Docker
+102
View File
@@ -0,0 +1,102 @@
OPTIND=1
TUNNEL_NAME="default"
TUNNEL_ID=""
DOMAIN="example.com"
CLOUDFLARE_CONFIG="$HOME/.cloudflared/config.yaml"
SUBDOMAINS=()
PORTS=()
PORT=1111
VERBOSE=0
while getopts "d:p:n?s?c?v?h?" opt; do
case "$opt" in
h|\?)
echo "A utility script for quickly getting up and started with self-hosted applicaitons."
echo "This script assumes the following:"
echo "- You have already signed in to the cloudflared CLI application;"
echo "- You own a domain through Cloudflare;"
echo ""
echo " |key-value pairs of sub and port"
echo "Usage: $0 [d domain] [p port] {n tunnel_name} {s subdomain=1234}"
echo " {c cloudflare_config} {v verbose} {h help}"
exit 0
;;
n) TUNNEL_NAME=$OPTARG
;;
d) DOMAIN=$OPTARG
;;
c) CLOUDFLARE_CONFIG=$OPTARG
;;
v) VERBOSE=1
;;
s) SUBDOMAINS+=("$(echo $OPTARG | cut -f1 -d=)")
PORTS+=("$(echo $OPTARG | cut -f2 -d=)")
;;
p) PORT="$OPTARG"
;;
esac
done
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
# TODO: Install dependencies.
# TODO: Add CLI option to enable cloudflared
# Initialize cloudflared
#cloudflared tunnel login
if [[ "$VERBOSE" -eq 1 ]]; then
echo "Creating cloudflared tunnel..."
fi
output=$(cloudflared tunnel create $TUNNEL_NAME)
TUNNEL_ID=$(echo "$output" | grep -o 'id [^ ]*' | awk -F ' ' '{print $2}')
if [[ "$VERBOSE" -eq 1 ]]; then
echo "Created cloudflared tunnel, ID $TUNNEL_ID"
fi
if [[ "$VERBOSE" -eq 1 ]]; then
echo "Creating cloudflared tunnel credentials file..."
fi
echo "tunnel: $TUNNEL_ID" > $CLOUDFLARE_CONFIG
echo "credentials-file: $HOME/.cloudflared/$TUNNEL_ID.json" >> $CLOUDFLARE_CONFIG
echo "ingress:" >> $CLOUDFLARE_CONFIG
# Add subdomain and associated ports
len=${#SUBDOMAINS[@]}
for (( i=0; i<${len}; i++ ));
do
echo ${SUBDOMAINS[$i]}
echo ${PORTS[$i]}
done
echo $SUBDOMAINS
for (( i=0; i<${len}; i++ ));
do
echo " - hostname: ${SUBDOMAINS[$i]}.$DOMAIN" >> $CLOUDFLARE_CONFIG
echo " service: http://127.0.0.1:${PORTS[$i]}" >> $CLOUDFLARE_CONFIG
if [[ "$VERBOSE" -eq 1 ]]; then
echo "Associated ${SUBDOMAINS[$i]}.$DOMAIN to port ${PORTS[$i]}"
fi
done
echo " - hostname: $DOMAIN" >> $CLOUDFLARE_CONFIG
echo " service: http://127.0.0.1:$PORT" >> $CLOUDFLARE_CONFIG
echo " - service: http_status:404" >> $CLOUDFLARE_CONFIG
echo "Associated $DOMAIN to port $PORT"
if [[ "$VERBOSE" -eq 1 ]]; then
echo "Created cloudflared tunnel credentials file"
cat $CLOUDFLARE_CONFIG
fi
if [[ "$VERBOSE" -eq 1 ]]; then
echo "Routing domains and sub-domains to the tunnel..."
fi
for (( i=0; i<${len}; i++ ));
do
cloudflared tunnel route dns $TUNNEL_ID ${SUBDOMAINS[$i]}.$DOMAIN
done
cloudflared tunnel route dns $TUNNEL_ID $DOMAIN
cloudflared tunnel run $TUNNEL_NAME
if [[ "$VERBOSE" -eq 1 ]]; then
echo "Complete!"
fi
@@ -113,7 +113,7 @@ func sendResponse(w http.ResponseWriter, encryptedMessage Communication) {
// If there's been no data corruption, re-construct user prompt for ollama // If there's been no data corruption, re-construct user prompt for ollama
// TODO: Eventually, add additional logic that will allow for re-direction and contextual awareness (pre-processing) // TODO: Eventually, add additional logic that will allow for re-direction and contextual awareness (pre-processing)
func chat(w http.ResponseWriter, r *http.Request) { func chat(w http.ResponseWriter, r *http.Request) {
model := "qwen:0.5b" // qwen:0.5b used for testing while hosting from my laptop. llama3 seems to be the best to use normally. model := "qwen2:1.5b" // qwen:1.5b used for testing while hosting from my laptop. llama3 seems to be the best to use normally.
input, err := io.ReadAll(r.Body) input, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
@@ -128,7 +128,7 @@ func chat(w http.ResponseWriter, r *http.Request) {
// TODO: Add support for dynamically changing models. // TODO: Add support for dynamically changing models.
switch prompt.Filetype { switch prompt.Filetype {
case "txt": case "txt":
model = "qwen:0.5b" model = "qwen2:1.5b"
case "img": case "img":
model = "llava" model = "llava"
case "doc": case "doc":
@@ -175,7 +175,7 @@ func chat(w http.ResponseWriter, r *http.Request) {
} }
func generate(w http.ResponseWriter, r *http.Request) { func generate(w http.ResponseWriter, r *http.Request) {
model := "qwen:0.5b" // qwen:0.5b used for testing while hosting from my laptop. llama3 seems to be the best to use normally. model := "qwen2:1.5b" // qwen2:1.5b used for testing while hosting from my laptop. llama3 seems to be the best to use normally.
input, err := io.ReadAll(r.Body) input, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
sendError(w, "Error reading request.") sendError(w, "Error reading request.")
@@ -195,7 +195,7 @@ func generate(w http.ResponseWriter, r *http.Request) {
// TODO: Add support for dynamically changing models. // TODO: Add support for dynamically changing models.
switch prompt.Filetype { switch prompt.Filetype {
case "txt": case "txt":
model = "qwen:0.5b" model = "qwen:1.5b"
case "img": case "img":
model = "llava" model = "llava"
case "doc": case "doc":
@@ -298,7 +298,7 @@ func serve() {
func parseCLI() { func parseCLI() {
// Define CLI option, shorthand, default value, and description // Define CLI option, shorthand, default value, and description
// TODO: Dynamically obtain default config location from environment variables. // TODO: Dynamically obtain default config location from environment variables.
pflag.StringP("config", "c", "/home/violet/.config/one-ai/default.json", "Configuration file used in initializing One AI.") pflag.StringP("config", "c", "/home/violet/.config/one-ai/test.json", "Configuration file used in initializing One AI.")
//pflag.StringP("help", "h", "false", "Display information about using the One AI CLI.") //pflag.StringP("help", "h", "false", "Display information about using the One AI CLI.")
pflag.Parse() pflag.Parse()
+9
View File
@@ -0,0 +1,9 @@
SUB=("api=1" "test=2" "portfolio=3")
arraylength=${#SUB[@]}
for (( i=0; i<${arraylength}; i++ ));
do
test="$(echo ${SUB[$i]} | cut -f1 -d=)"
test2="$(echo ${SUB[$i]} | cut -f2 -d=)"
echo $test $test2
done
@@ -1,5 +1,6 @@
#Sun Sep 01 20:23:42 MDT 2024
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-all.zip