-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathup
More file actions
executable file
·227 lines (184 loc) · 6.71 KB
/
up
File metadata and controls
executable file
·227 lines (184 loc) · 6.71 KB
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
#!/bin/bash
# Exit on any failure.
set -e
#######################################
##
## Verify and complete the following variables before
## running this script.
##
#######################################
# Go variables for installation location and project directory.
CWD=$(pwd)
GOPATH=$CWD/go
GOBIN=$CWD/go/bin
GOROOT=$CWD/go1.8/go
PATH=$GOROOT/bin:$GOBIN:$PATH
# URL containing tools. Leave off 'http://' and 'https://'.
GOURL=storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz
GLIDEURL=ppa.launchpad.net/masterminds/glide/ubuntu/pool/main/g/glide/glide_0.12.3~xenial.tar.gz
EXAMPLEURL=github.com/halverneus/example
# Installation configuration.
CONFIGFILE=$CWD/example/etc/example/example.yaml
STORAGEDIR=$CWD/example/var/lib/example/storage/
DATABASE=$CWD/example/var/lib/example/example.db
BIND=":8080"
# BIND="127.0.0.1:8080"
VERSION=0.4.0
# setup a new environment from scratch.
setup() {
# Confirm directory is empty except for this script.
for entry in $(ls -A1 $CWD); do
if [ "$entry" != "up" ]; then
printf 'Directory contains: %s\nDirectory must be empty except for "up".\n' "$entry"
exit 1
fi
done
# Creating a file for sourcing. File is reentrant and can be called multiple times.
cat <<EOF > source.me.sh
# Reentrant source file for setting up environment variables.
export GOPATH=$CWD/go
export GOBIN=$CWD/go/bin
export GOROOT=$CWD/go1.8/go
if [[ "\$PATH" != *"$CWD/go1.8/go/bin:$CWD/go/bin"* ]]; then
export PATH=\$GOROOT/bin:\$GOBIN:\$PATH
fi
EOF
# Create file system layout.
mkdir -p $(dirname "${CONFIGFILE}")
mkdir -p $STORAGEDIR
mkdir -p $(dirname "${DATABASE}")
mkdir -p $GOPATH/bin
mkdir -p $GOPATH/pkg
mkdir -p $GOPATH/src
mkdir -p $GOROOT
# Create configuration file.
cat <<EOF > $CONFIGFILE
database:
filename: $DATABASE
storage:
folder: $STORAGEDIR
example:
bind: $BIND
EOF
# Download Go 1.8.
gofile=$(basename "${GOURL}")
wget http://$GOURL
tar -C $GOROOT/.. -xzf $gofile
rm $gofile
# Download Glide.
glidefile=$(basename "${GLIDEURL}")
wget http://$GLIDEURL
tar -C $GOBIN -xzf $glidefile xenial/debian/amd64/glide
mv $GOBIN/xenial/debian/amd64/glide $GOBIN/glide
rm -rf $GOBIN/xenial
rm $glidefile
# Loading source file to access 'go'.
source source.me.sh
# Download Example
go get -u $EXAMPLEURL/bin/example
go install -ldflags "-X $EXAMPLEURL/cli.Version=$VERSION" $EXAMPLEURL/bin/example
complete="
===========================================================================
Installation is complete. The 'example' executable is in your GOBIN
directory. Type 'source source.me.sh' to put GOBIN in your PATH.
Next steps:
- Run './up init' to set up the first user.
- Run './up run' to execute the server.
WARNING: Do NOT run './up setup' again without running './up wipe'
first. Running './up wipe' will remove all data.
===========================================================================
"
echo "$complete"
}
case $1 in
setup) # One-time installation script.
setup
;;
init) # Create a new user. Reserved for additional setup.
source source.me.sh
# Ask for credentials for setting up a new user. User gets 3 tries.
COUNT=0
while [[ -z $user ]] || [[ 8 -gt "${#pass1}" ]] || [ "$pass1" != "$pass2" ] || \
[[ "${#pass1}" -ne "${#pass2}" ]]
do
# Tracking number of retries while displaying a message on each failure.
let COUNT+=1
case $COUNT in
1)
echo "Let's set up your first user. Please enter your user name."
;;
2)
echo "Enter a password with at least 8 characters and a user name with at least one."
;;
3)
echo "Last chance to enter a valid user name and password."
;;
4)
echo "Failed to supply and appropriate user name and password."
exit 1
;;
esac
# Request credentials from user.
read -p "Enter user name: " user
read -s -p "Enter password: " pass1
echo ""
read -s -p "Re-enter password: " pass2
echo ""
done
# Initialize configuration file and settings, then add user.
example --config $CONFIGFILE user add $user $pass1
;;
update) # Checks for updates to 'example' to dependencies.
source source.me.sh
go get -u $EXAMPLEURL/bin/example
cd $GOPATH/src/$EXAMPLEURL
glide up
go install -ldflags "-X $EXAMPLEURL/cli.Version=$VERSION" $EXAMPLEURL/bin/example
;;
build) # Create a new build and places it in the GOBIN directory.
source source.me.sh
go install -ldflags "-X $EXAMPLEURL/cli.Version=$VERSION" $EXAMPLEURL/bin/example
;;
run) # Starts the server.
source source.me.sh
example --config $CONFIGFILE run
;;
test) # Run all unit tests.
source source.me.sh
cd $GOPATH/src/$EXAMPLEURL
go test -cover $(glide novendor)
;;
wipe) # Remove all files and folders except for this script.
find . ! -name 'up' -type f -exec rm -f {} + &> /dev/null
find . ! -name 'up' -type d -exec rm -rf {} + &> /dev/null
;;
*) # Bad command or help. Printing help to the screen.
helpText="
Usage: up { setup | init | update | build | run | test | wipe }
setup: Create an empty directory. It is okay to have this script in the
directory. cd into the directory and execute './up setup'. This will
install Go, all Example dependencies and Example.
init: Performs initial setup routines, including creating a new user.
update: This will pull down the latest Example code, update dependencies
and reinstall Example.
build: If any changes are made to the source code, this will build it and
place it in the GOBIN directory.
run: Starts the server.
test: Performs all unit tests on the code.
wipe: Deletes all files except for the 'up' file.
NOTE: All steps install into GOBIN.
NOTE: This script creates a file called 'source.me.sh' that is
useful for letting you use Go from the command line. Just
run 'source source.me.sh' before running any 'go' commands.
FIRST TIME USER? Run these commands.
mkdir emptydir
cd emptydir
cp folder/with/up ./
./up setup # Downloads and installs everything in one directory.
./up init # Command will as for credentials to create a new user.
./up run # Ctrl+C to exit.
"
echo "$helpText"
exit 1
;;
esac