forked from refraction-networking/conjure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prereqs_once.sh
executable file
·194 lines (157 loc) · 5 KB
/
prereqs_once.sh
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
#!/bin/bash
#
# Set up basic prereqs for building and running Conjure
# Run this once per new machine. See README.md
TMPDIR=/tmp/cj-prereqs
# For CI, allow running as root
# But for normal installs, running as root will usually cause problems,
# as your .cargo directory will be owned by root.
# Symptoms: make (cargo build) fails when run by non-root.
if [ $(id -u) -eq 0 ]; then
alias sudo=''
fi
sudo rm -rf "${TMPDIR}"
mkdir -p "${TMPDIR}"
cd "${TMPDIR}"
if [ $? -ne 0 ]; then
echo "$0: failed to create tmpdir ${TMPDIR}"
exit 1
fi
isDocker(){
local cgroup=/proc/1/cgroup
test -f $cgroup && [[ "$(<$cgroup)" = *:cpuset:/docker/* ]]
}
isDockerBuildkit(){
local cgroup=/proc/1/cgroup
test -f $cgroup && [[ "$(<$cgroup)" = *:cpuset:/docker/buildkit/* ]]
}
isDockerContainer(){
[ -e /.dockerenv ]
}
install_deps() {
echo "INSTALLING DEPENDENCIES..."
sudo apt-get update && sudo apt-get install -yf automake build-essential bison flex \
libtool libpcap-dev \
libnuma-dev libargtable2-dev lunzip \
python python-protobuf protobuf-compiler \
libprotobuf-dev golang-protobuf-extensions-dev \
daemontools libzmq3-dev pkg-config curl
if [ -z ${is_docker_build+x} ]; then
sudo apt-get install -yf linux-virtual
else
sudo apt-get install -yf linux-headers-$(uname -r)
fi
if [ $? -ne 0 ]; then
echo "$0: installing packages failed"
exit 1
fi
}
fetch_file() {
# Pull a file out of the local cache, if we can find it.
# Otherwise, fetch it from the given URL.
#
# If the filename and the URL don't match, you'll probably
# get garbage. This is not detected right now.
if [ ! ${TMPDIR+x} ]; then
echo "$0: TMPDIR not set in fetch_file"
exit 1
fi
local _DESTDIR="${TMPDIR}"
local _URL="$1"
local _FNAME="$2"
# Get rid of any previous version of the file
#
sudo rm -f "${_DESTDIR}/${_FNAME}"
if [ ! -d "${_DESTDIR}" ]; then
mkdir -p "${_DESTDIR}"
fi
if [ -f "${CACHEDIR}/${_FNAME}" ]; then
echo "$0: using cached copy of ${_FNAME}"
cp "${CACHEDIR}/${_FNAME}" "${_DESTDIR}/${_FNAME}"
else
echo "$0: fetching ${_FNAME} from ${_URL}"
cd "${_DESTDIR}"
wget -nv "${_URL}"
fi
if [ ! -f "${_DESTDIR}/${_FNAME}" ]; then
echo "$0: failed to get file ${_FNAME}"
exit 1
fi
}
install_go() {
# INSTALL GOLANG
if ! command -v go &> /dev/null; then
GOLANG_LATEST_STABLE_VERSION=$(curl "https://go.dev/dl/?mode=json" | jq -r '.[0].files[].filename | select(test("go.*.linux-amd64.tar.gz"))')
echo "unable to find golang, installing latest. ${GOLANG_LATEST_STABLE_VERSION}"
curl -OL https://go.dev/dl/${GOLANG_LATEST_STABLE_VERSION}
if [ $? -ne 0 ]; then
echo "$0: pulling golang latest stable failed"
exit 1
fi
tar -C /usr/local -xzf ${GOLANG_LATEST_STABLE_VERSION}
export PATH=$PATH:/usr/local/go/bin
if ! command -v go &> /dev/null; then
echo "$0: failed to install golang"
exit 1
fi
go version
fi
}
install_rust() {
# INSTALL RUST
# if you already have a version of RUST installed, it seems to be
# easily confused by things in your $HOME/.cargo directory that
# correspond to that version. So blow that directory away, if there
# is one. This will make your first build after this slower than
# normal (but if you've never done a build before, it won't change
# anything)
# NOTE: if you already have already run RUST in the current shell,
# it may have exported environment variables which are not, for
# some reason, reset later. So you probably want to create a new
# shell in which to run later commands. TODO: there must be a
# better way.
cd "${TMPDIR}"
curl -sSf https://sh.rustup.rs > install_rustup.sh
chmod +x install_rustup.sh
./install_rustup.sh --default-toolchain=stable -y
if [ $? -ne 0 ]; then
echo "$0: installing rust failed"
exit 1
fi
source "$HOME/.cargo/env"
# cargo install --vers 1.4.5 protobuf
# if [ $? -ne 0 ]; then
# echo "$0: installing protobuf for rust failed"
# exit 1
# fi
}
install_routes() {
sudo apt-get -yf install iproute2
# install custom route priority, if not already done
#
if [ $(grep -c "200 custom" /etc/iproute2/rt_tables) -eq 0 ]; then
sudo /bin/sh -c "echo 200 custom >> /etc/iproute2/rt_tables"
fi
}
echo "Installing install, libssl, git, cargo..."
sudo apt-get update -y
# FIXME: Hold back kernel and kernel headers?
sudo apt-get install -y libssl-dev git libgmp3-dev wget lsb-release build-essential jq
install_deps
# /var/lib/conjure is hardcoded as the location of the client_config
# and overloaded_decoys files
# So we use it also for the per-machine configs
# and as the default directory for the station private key
if [ ! -d "/var/lib/conjure" ]; then
echo "Creating /var/lib/conjure"
sudo mkdir -p /var/lib/conjure
if [ $? -ne 0 ]; then
echo "$0: Could not create /var/lib/conjure"
exit 1
fi
fi
install_rust
install_routes
install_go
echo "CONJURE PREREQS INSTALLED"
exit 0