|
4 | 4 |
|
5 | 5 | set -e
|
6 | 6 |
|
7 |
| -# clear screen |
8 |
| -printf "\033c" |
9 |
| - |
10 |
| - |
11 |
| -# define functions |
12 |
| - |
13 |
| - |
14 | 7 | fg_cyan="\033[36m"
|
15 | 8 | bold_fg_white="\033[1;37m"
|
16 | 9 | bg_red="\033[41m"
|
17 | 10 | reset="\033[0m"
|
18 | 11 |
|
19 | 12 | error() {
|
20 |
| - # shellcheck disable=SC2059 |
21 |
| - printf "${bg_red}${bold_fg_white}%s %s${reset}\n" "[-]" "$*" 1>&2 |
| 13 | + # shellcheck disable=SC2059 |
| 14 | + printf "${bg_red}${bold_fg_white}%s %s${reset}\n" "[-]" "$*" 1>&2 |
22 | 15 | }
|
23 | 16 |
|
24 | 17 | info() {
|
25 |
| - # shellcheck disable=SC2059 |
26 |
| - printf "${fg_cyan}%s %s${reset}\n" "[*]" "$*" |
| 18 | + # shellcheck disable=SC2059 |
| 19 | + printf "${fg_cyan}%s %s${reset}\n" "[*]" "$*" |
27 | 20 | }
|
28 | 21 |
|
| 22 | +get_key() { |
| 23 | + head -c 32 /dev/urandom | base64 -w 0 |
| 24 | +} |
29 | 25 |
|
30 |
| -# shellcheck disable=SC2059 |
31 |
| -printf "\033[1;36m....########......######.....########....########... |
32 |
| -....##.....##....##....##.......##.......##......... |
33 |
| -....##.....##....##.............##.......##......... |
34 |
| -....########.....##.............##.......######..... |
35 |
| -....##...##......##.............##.......##......... |
36 |
| -....##....##.....##....##.......##.......##......... |
37 |
| -....##.....##.....######........##.......##......... |
38 |
| -
|
39 |
| -${reset}" |
40 |
| - |
41 |
| - |
42 |
| -# check environment |
43 |
| - |
44 |
| - |
45 |
| -info "Checking environment..." |
46 |
| - |
47 |
| -if [ ! "$(id -u)" = 0 ]; then |
48 |
| - error "You must run this script as root." |
49 |
| - exit 1 |
50 |
| -fi |
51 |
| - |
52 |
| -if [ -x "$(command -v apt-get)" ]; then |
53 |
| - PACKAGE_MANAGER="apt-get" |
54 |
| -elif [ -x "$(command -v yum)" ]; then |
55 |
| - error "Warning: Support for RHEL-like distros is experimental and things might break. Giving you 10 seconds to change your mind (by pressing Ctrl+C)..." |
56 |
| - sleep 10 |
57 |
| - |
58 |
| - PACKAGE_MANAGER="yum" |
59 |
| -elif [ -x "$(command -v pacman)" ]; then |
60 |
| - info "redpwn uses arch too btw" |
61 |
| - |
62 |
| - PACKAGE_MANAGER="pacman" |
63 |
| -else |
64 |
| - # XXX: should we stop the script here? |
65 |
| - error "Unable to identify usable package manager, ignoring dependencies for now."; |
66 |
| -fi |
67 |
| - |
68 |
| - |
69 |
| -# setup variables |
70 |
| - |
| 26 | +do_install() { |
| 27 | + info "Installing rCTF..." |
71 | 28 |
|
72 |
| -info "Configuring installation..." |
| 29 | + if [ ! "$(id -u)" = 0 ]; then |
| 30 | + error "You must run this script as root." |
| 31 | + exit 1 |
| 32 | + fi |
73 | 33 |
|
74 |
| -INSTALL_PATH="${INSTALL_PATH:-"/opt/rctf"}" |
| 34 | + RCTF_INSTALL_PATH="${RCTF_INSTALL_PATH:-"/opt/rctf"}" |
75 | 35 |
|
76 |
| -if [ ! -d "$(dirname "$INSTALL_PATH")" ]; then |
77 |
| - error "The parent of \$INSTALL_PATH ($(dirname "$INSTALL_PATH")) does not exist." |
| 36 | + if [ ! -d "$(dirname "$RCTF_INSTALL_PATH")" ]; then |
| 37 | + error "The parent of \$RCTF_INSTALL_PATH ($(dirname "$RCTF_INSTALL_PATH")) does not exist." |
78 | 38 | exit 1
|
79 |
| -fi |
| 39 | + fi |
80 | 40 |
|
81 |
| -if [ -d "$INSTALL_PATH" ]; then |
82 |
| - error "rCTF appears to already be installed in ${INSTALL_PATH}" |
| 41 | + if [ -d "$RCTF_INSTALL_PATH" ]; then |
| 42 | + error "rCTF appears to already be installed in ${RCTF_INSTALL_PATH}" |
83 | 43 |
|
84 |
| - info "... If you're trying to start rCTF, run 'docker-compose up -d --project-directory $INSTALL_PATH'." |
85 |
| - info "... If you're trying to reinstall rCTF, 'rm -rf $INSTALL_PATH' then re-run this script." |
| 44 | + info "... If you're trying to start rCTF, run 'docker-compose up -d'." |
| 45 | + info "... If you're trying to reinstall rCTF, 'rm -rf $RCTF_INSTALL_PATH' then re-run this script." |
86 | 46 |
|
87 | 47 | exit 1
|
88 |
| -fi |
89 |
| - |
90 |
| -REPOSITORY_URL="${REPOSITORY_URL:-"https://github.com/redpwn/rCTF.git"}" |
91 |
| -REPOSITORY_BRANCH="${REPOSITORY_BRANCH:-"master"}" |
92 |
| - |
93 |
| - |
94 |
| -# install dependencies |
95 |
| - |
| 48 | + fi |
96 | 49 |
|
97 |
| -info "Installing dependencies..." |
| 50 | + mkdir "$RCTF_INSTALL_PATH" |
| 51 | + cd "$RCTF_INSTALL_PATH" |
98 | 52 |
|
99 |
| -if [ "$PACKAGE_MANAGER" = "apt-get" ]; then |
100 |
| - apt-get update |
101 |
| - apt-get install --yes docker.io docker-compose git |
102 |
| -elif [ "$PACKAGE_MANAGER" = "yum" ]; then |
103 |
| - info "We are about to install docker via https://get.docker.com/. Please follow along the steps to ensure it is configured properly." |
| 53 | + info "Installing dependencies..." |
104 | 54 |
|
105 |
| - # pass Ctrl+C / SIGINT to inside script |
106 |
| - sh -c ' |
107 |
| - trap break INT |
108 |
| - curl -fsSL https://get.docker.com/ | sh |
109 |
| - ' |
| 55 | + if [ ! -x "$(command -v docker)" ]; then |
| 56 | + curl https://get.docker.com | sh |
| 57 | + fi |
110 | 58 |
|
111 |
| - yum install git |
112 |
| -elif [ "$PACKAGE_MANAGER" = "pacman" ]; then |
113 |
| - pacman -Sy --noconfirm --needed docker docker-compose git |
114 |
| -fi |
| 59 | + if [ ! -x "$(command -v docker-compose)" ]; then |
| 60 | + curl -fsSLo /usr/local/bin/docker-compose "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" |
| 61 | + chmod +x /usr/local/bin/docker-compose |
| 62 | + fi |
115 | 63 |
|
116 |
| -info "Enabling docker..." |
| 64 | + info "Configuring rCTF..." |
117 | 65 |
|
118 |
| -(systemctl enable docker || true) 2>/dev/null # XXX: Debian "masks" docker.service |
119 |
| -(systemctl start docker || true) 2>/dev/null |
| 66 | + RCTF_GIT_COMMIT="${RCTF_GIT_COMMIT:-"{{git_commit}}"}" |
120 | 67 |
|
| 68 | + mkdir -p conf.d data/rctf-postgres data/rctf-redis |
121 | 69 |
|
122 |
| -# clone repository |
| 70 | + printf "%s\n" \ |
| 71 | + "RCTF_DATABASE_PASSWORD=$(get_key)" \ |
| 72 | + "RCTF_REDIS_PASSWORD=$(get_key)" \ |
| 73 | + "RCTF_GIT_COMMIT=$RCTF_GIT_COMMIT" \ |
| 74 | + > .env |
123 | 75 |
|
| 76 | + printf "%s\n" \ |
| 77 | + "ctfName: rCTF" \ |
| 78 | + "meta:" \ |
| 79 | + " description: 'A description of your CTF'" \ |
| 80 | + " imageUrl: 'https://example.com'" \ |
| 81 | + "homeContent: 'A description of your CTF. Markdown supported.'" \ |
| 82 | + > conf.d/01-ui.yaml |
124 | 83 |
|
125 |
| -info "Cloning repository to ${INSTALL_PATH} from repository ${REPOSITORY_URL} branch ${REPOSITORY_BRANCH}..." |
| 84 | + printf "%s\n" \ |
| 85 | + "origin: http://127.0.0.1:8080" \ |
| 86 | + "divisions:" \ |
| 87 | + " open: Open" \ |
| 88 | + "tokenKey: '$(get_key)'" \ |
| 89 | + "startTime: $(date +%s)000" \ |
| 90 | + "endTime: $(date -d +1week +%s)000" \ |
| 91 | + > conf.d/02-ctf.yaml |
126 | 92 |
|
127 |
| -git clone "$REPOSITORY_URL" "$INSTALL_PATH" |
128 |
| -cd "$INSTALL_PATH" |
129 |
| -git checkout "$REPOSITORY_BRANCH" |
| 93 | + info "Downloading rCTF..." |
130 | 94 |
|
| 95 | + curl -fsSO "https://raw.githubusercontent.com/redpwn/rctf/$RCTF_GIT_COMMIT/docker-compose.yml" |
| 96 | + docker-compose pull |
131 | 97 |
|
132 |
| -# configure rctf |
| 98 | + info "Finished installation to ${RCTF_INSTALL_PATH}." |
133 | 99 |
|
| 100 | + printf "Would you like to start rCTF now (y/N)? " |
134 | 101 |
|
135 |
| -info "Configuring rCTF..." |
| 102 | + read -r result </dev/tty |
136 | 103 |
|
137 |
| -./install/config.sh |
138 |
| - |
139 |
| - |
140 |
| -# start docker |
141 |
| - |
142 |
| - |
143 |
| -info "Finished installation to ${INSTALL_PATH}." |
144 |
| - |
145 |
| -printf "Would you like to start rCTF now (y/N)? " |
146 |
| - |
147 |
| -read -r result </dev/tty |
| 104 | + if [ "$result" = "y" ]; then |
| 105 | + info "Running 'docker-compose up -d'..." |
| 106 | + docker-compose up -d |
| 107 | + info "rCTF is now running at 127.0.0.1:8080." |
| 108 | + exit 0 |
| 109 | + else |
| 110 | + info "If you would like to start rCTF, run 'docker-compose up -d' in $RCTF_INSTALL_PATH." |
| 111 | + exit 0 |
| 112 | + fi |
| 113 | +} |
148 | 114 |
|
149 |
| -if [ "$result" = "y" ]; then |
150 |
| - info "Running 'docker-compose up -d'..." |
151 |
| - docker-compose up -d |
152 |
| - info "rCTF is now running at 127.0.0.1:8080." |
153 |
| - exit 0 |
154 |
| -else |
155 |
| - info "If you would like to start rCTF, run 'docker-compose up -d' in $INSTALL_PATH." |
156 |
| - exit 0 |
157 |
| -fi |
| 115 | +do_install |
0 commit comments