-
Notifications
You must be signed in to change notification settings - Fork 0
/
container
executable file
·102 lines (84 loc) · 2.43 KB
/
container
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
#!/bin/bash
set -e
set -u
set -o pipefail
# ---------------------------------------------------------- #
# Configuration
# ---------------------------------------------------------- #
CONTAINER=test-container
SSH_PORT=4022
HTTP_PORT=4080
LSADM_PORT=4070
# ---------------------------------------------------------- #
# Defaults
# ---------------------------------------------------------- #
COLOR_RED='\033[0;31m'
COLOR_NONE='\033[0m'
# ---------------------------------------------------------- #
# Functions
# ---------------------------------------------------------- #
function log {
DATETIME=$(date '+%Y/%m/%d %H:%M:%S')
echo -e "[STACK][$DATETIME] $1"
}
function error {
log "${COLOR_RED}$1${COLOR_NONE}"
exit 1
}
function init {
lxc launch ubuntu:jammy $CONTAINER || return 1
lxc config device add $CONTAINER ssh proxy listen=tcp:0.0.0.0:$SSH_PORT connect=tcp:127.0.0.1:22 || return 1
lxc config device add $CONTAINER http proxy listen=tcp:0.0.0.0:$HTTP_PORT connect=tcp:127.0.0.1:80 || return 1
lxc config device add $CONTAINER lsadm proxy listen=tcp:0.0.0.0:$LSADM_PORT connect=tcp:127.0.0.1:7080 || return 1
sleep 5
chmod -R 600 ./ssh/id_ed25519
lxc file push ./ssh/authorized_keys $CONTAINER/root/.ssh/authorized_keys
lxc exec $CONTAINER -- chown root:root /root/.ssh/authorized_keys || return 1
}
function provision {
ansible-playbook provision.yml -e host=test-container "${@}" || return 1
}
function shell {
lxc exec $CONTAINER bash || return 1
}
function wp-pull {
ansible-playbook playbooks/wordpress/pull.yml -e host=test-container "${@}" || return 1
}
function snapshot {
lxc snapshot $CONTAINER || return 1
}
function restore {
lxc restore $CONTAINER $1 || return 1
}
function destroy {
lxc delete $CONTAINER -f || return 1
}
# ---------------------------------------------------------- #
# Commands
# ---------------------------------------------------------- #
case "$1" in
init)
init || error "Failed to initialize test container."
;;
provision)
provision "${@:2}" || error "Failed to provision test container."
;;
shell)
shell || error "Failed to start a shell."
;;
wp-pull)
wp-pull "${@:2}" || error "Failed to pull WordPress to test container."
;;
snapshot)
snapshot || error "Failed to create a snapshot."
;;
restore)
restore $2 || error "Failed to restore a snapshot."
;;
destroy)
destroy || error "Failed to destroy test container."
;;
*)
log "No command specified."
;;
esac