forked from AlexsLemonade/refinebio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_nomad.sh
executable file
·149 lines (126 loc) · 4.41 KB
/
run_nomad.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
#!/bin/bash
# This script runs the Data Refinery Workers locally.
# What that actually means is that it runs Nomad and registers the
# Nomad jobs for the Data Refinery Workers. Nomad is listening for
# messages telling it to run Processor and Downloader jobs. When it
# receives them, it will run the jobs within new Docker containers.
print_description() {
echo "Starts Nomad and registers the jobs with it. This involves re-building the "
echo "Docker images and running format_nomad_with_env.sh to format the Nomad job "
echo "specifications for the correct environment."
}
print_options() {
echo "Options:"
echo " -h Prints the help message"
echo " -e ENVIRONMENT The environment to start. 'local' is the default option."
echo " The other options are 'prod' and 'test'"
}
while getopts ":e:v:h" opt; do
case $opt in
e)
env=$OPTARG
;;
v)
export system_version=$OPTARG
;;
h)
print_description
echo
print_options
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
print_options >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
print_options >&2
exit 1
;;
esac
done
if [[ -z $env ]]; then
env="local"
fi
if [[ -z $system_version ]]; then
system_version="latest"
fi
# Figure out the right location to put the nomad directory.
script_directory=`perl -e 'use File::Basename;
use Cwd "abs_path";
print dirname(abs_path(@ARGV[0]));' -- "$0"`
cd $script_directory
# Set up the data volume directory if it does not already exist
volume_directory="$script_directory/volume"
if [ ! -d "$volume_directory" ]; then
mkdir $volume_directory
chmod -R a+rwX $volume_directory
fi
# Load get_ip_address function.
source common.sh
export HOST_IP=$(get_ip_address)
# Use a different Nomad port for test environment to avoid interfering
# with the non-test Nomad.
export NOMAD_PORT=4646
if [ $env == "test" ]; then
export NOMAD_PORT=5646
# format_nomad_with_env.sh will create distinct test Nomad job
# specifications to avoid overwriting other job
# specifications. This is done by appending '.test' to the job
# specification file names.
export TEST_POSTFIX="_test"
# Additional Nomad configuration is necessary to avoid conflicts
# with non-test Nomad agent.
export TEST_NOMAD_CONFIG="-config=test_nomad_config.hcl"
else
# This is only for running Nomad in non-cloud environments so if
# its not test then we're in local
env="local"
fi
nomad_dir="$script_directory/nomad_dir$TEST_POSTFIX"
if [ ! -d $nomad_dir ]; then
mkdir $nomad_dir
fi
# Start Nomad in both server and client mode locally
nomad agent -bind $HOST_IP \
-data-dir $nomad_dir $TEST_NOMAD_CONFIG \
-config nomad_client.config \
-dev \
&> nomad.logs"$TEST_POSTFIX" &
export NOMAD_ADDR=http://$HOST_IP:$NOMAD_PORT
# While we wait for Nomad to start, make sure the Docker registry has
# an up-to-date Docker image for the workers sub-project. We run a
# local Docker registry because Nomad must pull from a registry, it
# will not use an image which lives on the host machine.
# Make sure the local Docker registry is running.
if [[ -z $(docker ps | grep "image-registry") ]]; then
docker run -d -p 5000:5000 --restart=always --name image-registry registry:2
fi
# This function checks what the status of the Nomad agent is.
# Returns an HTTP response code. i.e. 200 means everything is ok.
check_nomad_status () {
echo $(curl --write-out %{http_code} \
--silent \
--output /dev/null \
$NOMAD_ADDR/v1/status/leader)
}
# Wait for Nomad to get started.
nomad_status=$(check_nomad_status)
while [ $nomad_status != "200" ]; do
sleep 1
nomad_status=$(check_nomad_status)
done
echo "Nomad is online. Registering jobs."
./format_nomad_with_env.sh -p workers -e $env -v $system_version
./format_nomad_with_env.sh -p surveyor -e $env -v $system_version
# Register the jobs for dispatching.
for job_spec in $(ls -1 workers/nomad-job-specs/*.nomad$TEST_POSTFIX); do
echo "Registering $job_spec"
nomad run $job_spec
done
for job_spec in $(ls -1 foreman/nomad-job-specs/*.nomad$TEST_POSTFIX); do
echo "Registering $job_spec"
nomad run $job_spec
done