summaryrefslogtreecommitdiff
path: root/playbooks.d/webserver/playbook.bash
blob: 5c422f651fd59a7be98e96d5d181b3f8627125ce (plain)
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
#!/usr/bin/env bash

playbook_add()
{
	info "webserver/add" "Installing packages"
	pkg install nginx

	info "webserver/add" "Create www user"
	groupadd www
	useradd \
		--home-dir /var/www \
		--gid www \
		--system \
		--shell /sbin/nologin \
		www

	info "webserver/add" "Cleaning up whatever the package manager did"
	rm -frv -- "$(config "fs.etcdir")/nginx"

	info "webserver/add" "Creating desired directory structure"
	mkdir -pv -- "$(config "fs.etcdir")/nginx"
	mkdir -pv -- "$(config "fs.etcdir")/nginx/sites-available.d"
	mkdir -pv -- "$(config "fs.etcdir")/nginx/sites-enabled.d"
	mkdir -pv -- "$(config "fs.etcdir")/nginx/snippets.d"
	mkdir -pv -- /var/www

	info "webserver/add" "Running sync to get all configuration going"
	playbook_sync

	svc enable nginx
	svc start nginx
}

playbook_sync()
{
	local snippets
	local sites

	notice "webserver/sync" "Updating nginx.conf"
	file_template "nginx.conf" \
		etc="$(config "fs.etcdir")" \
		> "$(config "fs.etcdir")/nginx/nginx.conf"

	notice "webserver/sync" "Updating mime.types"
	file_template "mime.types" \
		etc="$(config "fs.etcdir")" \
		> "$(config "fs.etcdir")/nginx/mime.types"

	notice "webserver/sync" "Updating cert.sh"
	file_template "cert.sh" \
		> "$(config "fs.bindir")/cert.sh" \
		&& chmod +x "$(config "fs.bindir")/cert.sh"

	for path in "$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/share/snippets.d"/*.conf
	do
		snippet="$(basename "$path")"

		notice "webserver/sync" "Updating snippet $snippet"
		file_template "snippets.d/$snippet" \
			> "$(config "fs.etcdir")/nginx/snippets.d/$snippet"
	done

	for path in "$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/share/sites.d"/*
	do
		site="$(basename "$path")"

		notice "webserver/sync" "Updating site $site"
		file_template "sites.d/$site" \
			> "$(config "fs.etcdir")/nginx/sites-available.d/$site"
	done

	chown -R www:www "$(config "fs.etcdir")/nginx"

	[[ "$BASHTARD_COMMAND" == "add" ]] && return

	svc reload nginx
}

playbook_del()
{
	# Stop and remove the service
	svc stop nginx
	svc disable nginx

	# Clean up resources
	pkg uninstall nginx
	rm -fr -- /etc/nginx "$(config "fs.bindir")/cert.sh" /var/www/.acme
	userdel www
	groupdel www
}