~skiqqy/bin

ref: 3c0ea9d1382310297caced4fd4804dbbbb47ce66 bin/skiqqy -rwxr-xr-x 7.0 KiB
3c0ea9d1 — Stephen Cochrane Added touch on start 7 months ago
                                                                                
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env bash
# Simple script for managing skiqqy.xyz

domain='skiqqy.xyz' # Easy changing if I decide to switch domains.

# Dont Change These.
declare -A commands
commands[check]="check [-v] [SUBDOMAIN]\nCheck the status of a sub domain."
commands[list]="list\nShow avaible commands."
commands[help]="help COMMAND\nShow helpful information for a command, see:\n$ $(basename "$0") list"
commands[init]="init [-f]\nInit the configs."
commands[refresh]="refresh\nRefresh domains and subdomains IP's."
commands[nginxd]="nginxd ARGUMENTS...\nWrapper for nginxd."
commands[update]="update\nUpdate the server, pulling and rebuilding the website."
commands[error]="[ERROR] Command entered DNE, please see:\n$ $(basename "$0") list"
commands[deploy]="deploy [PORT]\nDeploy an instance of skiqqy.xyz in a container."

### Helper Functions ###

RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[92m'
NC='\033[0m'

# Usage: error MSG CODE
error() {
	printf "[%bERROR%b] $1\n" "$RED" "$NC"
	exit "$2"
}

# Usage: warning MSG
warning() {
	printf "[%bWARNING%b] $1\n" "$YELLOW" "$NC"
}

# Usage: success MSG
success() {
	printf "[%bSUCCESS%b] $1\n" "$GREEN" "$NC"
}

# Usage: tick MSG
tick() {
	printf "%s %b✔%b\n" "$1" "$GREEN" "$NC"
}

# Usage: http_code URL
http_code()
{
	local url
	url="${1:-"$domain"}"
	curl -s -o /dev/null --head -w "%{http_code}" "$url"
}

### Helper Functions ###

usage()
{
	cat << EOF
$(basename "$0") ~ Simple $domain maintenance script.

Usage:
$ $(basename "$0") [options] COMMAND ARGS...

Options:
-h               Shows this message.
-c DIRECTORY     Specify config directory.
-d DOMAIN        Specify the domain to operate on.

Commands:
$ $(basename "$0") list

Command Help:
$ $(basename "$0") help COMMAND

Written by Skiqqy
EOF
	exit "$1"
}

# Setup config file
sinit()
{
	local ret
	local force
	force=false
	ret=0

	while getopts 'f' opt
	do
		case "$opt" in
			f)
				force=true
				;;
			*)
				error "Invalid Flag" 1
				;;
		esac
	done
	shift "$((OPTIND-1))"

	mkdir -p "$confdir"

	if "$force" || [ ! -f "$confdir/config" ]
	then
		ret=1
		cat << EOF > "$confdir/config"
# Please Complete the config where necassary.
serv=$USER@localhost                 # ssh login for server.
nginxd=$USER@localhost               # ssh login for nginx server.
nginxd_pm=Backups/$USER@localhost    # Set to empty if ssh pass is not saved in pm instance.
serv_pm=Backups/$USER@localhost      # Set to empty if ssh pass is not saved in pm instance.
godaddy_key_pm=godaddy/key           # Set to empty if go daddy key is not saved in a pm instance.
godaddy_secret_pm=godaddy/secret     # Set to empty if go daddy secret is not saved in a pm instance.
container=skiqqy                     # Set to empty if not inside a container.
EOF
		printf 'Generated Config: %s\n' "$confdir/config"

		read -rp 'Edit config? (Y/N): ' input
		if [[ $input =~ ^(Y|y)((ES)|(es))?$ ]]
		then
			"$EDITOR" "$confdir/config"
			ret=0
		fi
	fi

	return "$ret"
}

# Print help info for a command.
shelp()
{
	local out
	local com

	com="${1:-help}"
	out="${commands[$com]}"
	[ -z "$out" ] && out="${commands[error]}"

	printf 'Usage: $ %s %b\n' "$(basename "$0")" "$out"
}

slist()
{
	local out
	out="${!commands[*]}"
	out="${out//error /}"
	printf 'Commands:\n\n%b\n' "${out// /\\n}"
}

scheck()
{
	local dom
	local verb

	while getopts 'v' opt
	do
		case "$opt" in
			v)
				verb=true
				;;
			*)
				error "Invalid Flag" 1
				;;
		esac
	done
	shift "$((OPTIND-1))"

	verb=${verb:-false}
	[ -n "$1" ] && dom="$1."

	if ! "$verb"
	then
		printf '%s: %s\n' "$dom$domain" "$(http_code "https://$dom$domain")"
	else
		curl -I "https://$dom$domain"
	fi
}

srefresh()
{
	local key
	local secret
	local url # Just temporary.
	url='https://raw.githubusercontent.com/skiqqy/skiqqy.github.io/master/dns.py' # Just temporary

	if [ -z "$godaddy_key_pm" ]
	then
		read -rp 'Enter GoDaddy Key: ' key
	else
		key="$(pm show "$godaddy_key_pm")"
	fi

	if [ -z "$godaddy_secret_pm" ]
	then
		read -rp 'Enter GoDaddy Secret: ' secret
	else
		secret="$(pm show "$godaddy_secret_pm")"
	fi

	python3 <(curl "$url") -k "$key" -s "$secret" # Just temporary.
}

snginxd()
{
	local run

	if [ -n "$nginxd" ]
	then
		# ssh to the nginx server.
		sshpass -p "$(pm show "$nginxd_pm")" ssh -t "$nginxd" \
			"
				npath=\$(find /var/www -name nginxd) # Find the Script
				npath=\"\${npath/nginxd/}\"          # Get Path
				cd \"\$npath\" || exit               # Exit if something went wrong
				sudo ./nginxd $*
			"
	else
		npath=$(find /var/www -name nginxd) # Find the Script
		npath="${npath/nginxd/}"            # Get Path
		cd "$npath" || return 1          # Exit if something went wrong
	fi
}

# Todo: Tidy up
supdate()
{
	local run
	run="cd /var/www && git pull && make" # "typical" command
	[ -n "$container" ] && run="sudo docker exec -it $container update"

	if [ -n "$serv_pm" ]
	then
		sshpass -p "$(pm show "$serv_pm")" ssh -t "$serv" "$run"
	else
		ssh -t "$serv" "$run"
	fi
}

sdeploy()
{
	local port
	local contd
	local url
	local gwiki

	# Set vars
	gwiki='https://git.sr.ht/~skiqqy/gwiki'
	url='https://github.com/skiqqy/skiqqy.github.io' # Repo url for skiqqy.xyz
	contd=
	port=${1:-81} # Deploy to port 81 by default.
	[[ ! $port =~ ^[0-9]+$ ]] && shelp deploy && return 1

	# Find container tool
	command -v podman > /dev/null && contd=podman
	[ -z "$contd" ] && command -v docker > /dev/null && contd=docker
	[ ! "$(whoami)" = root ] && [ "$contd" = docker ] && contd='sudo docker' # Assume sudo needed.
	[ -z "$contd" ] && \
		printf 'Cannot find a container tool, please install docker/podman' && \
		return 1

	# Print what will be done.
	printf 'Deploying skiqqy.xyz, using:\n\n\tContainer Env: %s\n\tPort: %s\n\tRepository: %s\n\n' \
		"$contd" "$port" "$url"

	git clone "$gwiki" /tmp/gwiki

	# Change working directories
	pushd /tmp/gwiki || return 1
		git pull # If /tmp/gwiki was not cloned (ie it was cloned previously) pull the latest
		printf '\nBuilding gwiki, this may take a while...\n\n'
		$contd build -t gwiki:latest .

		if [ -n "$(podman ps -af name=skiqqy | tail -n 1)" ]
		then
			echo TODO: Prompt user to ask if they want to delete old container.
		fi

		$contd run --name skiqqy \
			--restart=always \
			-p "$port":80 \
			--env CONFIG_HOME="$url" \
			--env TIMER=60 \
			gwiki:latest & # Fork the process, in the case of podman being used.
	popd || return 1
}

main()
{
	# Global Vars
	godaddy_secret_pm=
	godaddy_key_pm=
	serv=
	serv_pm=
	container=
	nginxd=
	nginxd_pm=

	while getopts 'hd:c:' opt
	do
		case "$opt" in
			h)
				usage 0
				;;
			d)
				domain="$OPTARG"
				;;
			c)
				confdir="$OPTARG"
				;;
			*)
				usage 1
				;;
		esac
	done
	confdir=${confdir:-"$HOME/.config/$domain"}

	shift "$((OPTIND - 1))" # Get rid of optargs

	if ! sinit
	then
		printf 'Please Complete the config file in %s\n' "$confdir/config"
		exit
	fi

	# shellcheck source=/dev/null
	. "$confdir/config" # Load config settings.
	if [ -z "$1" ]
	then
		usage 0
	elif [ -n "${commands[$1]}" ]
	then
		local args
		args="$1"
		shift 1

		s"$args" "$@" # Run the command
	else
		printf 'Invalid Command, see:\n$ %s list\n' "$(basename "$0")"
	fi
}

main "$@"