Skip to content

Commit 638c1cb

Browse files
committed
Numerous task and variable updates
- Use all directory variables in all templates - Addresses #8 - Addresses #9 - New variables: - `nomad_lockfile` - `nomad_run_dir` - Updated init script templates - Updated systemd unit template - Convert to local action plays
1 parent 41a5691 commit 638c1cb

File tree

8 files changed

+127
-126
lines changed

8 files changed

+127
-126
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,15 @@
179179

180180
- Nomad version 0.5.6
181181
- Update documentation
182+
183+
## v1.6.0
184+
185+
- Use all directory variables in all templates
186+
- Addresses #8
187+
- Addresses #9
188+
- New variables:
189+
- `nomad_lockfile`
190+
- `nomad_run_dir`
191+
- Updated init script templates
192+
- Updated systemd unit template
193+
- Convert to local action plays

defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ nomad_group_name: "cluster_nodes"
1818
nomad_bin_dir: "/usr/local/bin"
1919
nomad_config_dir: "/etc/nomad.d"
2020
nomad_data_dir: "/var/nomad"
21+
nomad_lockfile: "/var/lock/subsys/nomad"
2122
nomad_log_dir: "/var/log/nomad"
23+
nomad_run_dir: "/var/run/nomad"
2224
nomad_user: "root"
2325
nomad_group: "bin"
2426
nomad_region: "global"

tasks/install.yml

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,41 @@
99
tags: installation
1010

1111
- name: Check Nomad package checksum file
12+
local_action: stat path="{{ role_path }}/files/nomad_{{ nomad_version }}_SHA256SUMS"
1213
become: no
13-
connection: local
14-
stat:
15-
path: "{{ role_path }}/files/nomad_{{ nomad_version }}_SHA256SUMS"
1614
run_once: true
1715
register: nomad_checksum
1816

1917
- name: Get Nomad package checksum file
18+
local_action: get_url url="{{ nomad_checksum_file_url }}" dest="{{ role_path }}/files/nomad_{{ nomad_version }}_SHA256SUMS"
2019
become: no
21-
connection: local
22-
get_url:
23-
url: "{{ nomad_checksum_file_url }}"
24-
dest: "{{ role_path }}/files/nomad_{{ nomad_version }}_SHA256SUMS"
2520
run_once: true
2621
tags: installation
2722
when: nomad_checksum.stat.exists == False
2823

2924
- name: Get Nomad package checksum
25+
local_action: shell grep "{{ nomad_pkg }}" "{{ role_path }}/files/nomad_{{ nomad_version }}_SHA256SUMS" | awk '{print $1}'
3026
become: no
31-
connection: local
32-
shell: "grep {{ nomad_pkg }} {{ role_path }}/files/nomad_{{ nomad_version }}_SHA256SUMS"
3327
run_once: true
3428
register: nomad_sha256
3529
tags: installation
3630

3731
- name: Check Nomad package file
32+
local_action stat path="{{ role_path }}/files/{{ nomad_pkg }}"
3833
become: no
39-
connection: local
40-
stat:
41-
path: "{{ role_path }}/files/{{ nomad_pkg }}"
4234
run_once: true
4335
register: nomad_package
4436

4537
- name: Download Nomad
38+
local_actoin: get_url url="{{ nomad_zip_url }}" dest="{{ role_path }}/files/{{ nomad_pkg }}" checksum="sha256:{{ nomad_sha256.stdout }}" timeout="42"
4639
become: no
47-
connection: local
48-
get_url:
49-
url: "{{ nomad_zip_url }}"
50-
dest: "{{ role_path }}/files/{{ nomad_pkg }}"
51-
checksum: "sha256:{{ nomad_sha256.stdout.split(' ')|first }}"
52-
timeout: 42
5340
run_once: true
5441
tags: installation
5542
when: nomad_package.stat.exists == False
5643

5744
- name: Unarchive Nomad
45+
local_action: unarchive src="{{ role_path }}/files/{{ nomad_pkg }}" dest="{{ role_path }}/files/" creates="{{ role_path }}/files/nomad"
5846
become: no
59-
connection: local
60-
unarchive:
61-
src: "{{ role_path }}/files/{{ nomad_pkg }}"
62-
dest: "{{ role_path }}/files/"
63-
creates: "{{ role_path }}/files/nomad"
6447
run_once: true
6548
tags: installation
6649

@@ -74,11 +57,8 @@
7457
tags: installation
7558

7659
- name: Cleanup
60+
local_action: file path="{{ item }}" state="absent"
7761
become: no
78-
connection: local
79-
file:
80-
path: "{{ item }}"
81-
state: absent
8262
with_fileglob: "{{ role_path }}/files/nomad"
8363
run_once: true
8464
tags: installation

tasks/main.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@
5959
owner: "{{ nomad_user }}"
6060
group: "{{ nomad_group}}"
6161
with_items:
62-
- /opt/nomad
63-
- /var/nomad
64-
- /var/run/nomad
65-
- /etc/nomad.d
62+
- {{ nomad_data_dir }}
63+
- {{ nomad_config_dir }}
64+
- {{ nomad_log_dir }}
6665

6766
- name: Base configuration
6867
template:

templates/nomad_debian.init.j2

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,110 +8,115 @@
88
# Short-Description: distributed scheduler
99
# Description: distributed, highly available, datacenter-aware scheduler
1010
### END INIT INFO
11+
# shellcheck disable=SC2015
12+
# shellcheck disable=SC1090
13+
# shellcheck disable=SC1091
14+
# shellcheck disable=SC2034
1115

12-
PATH=/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
13-
DESC="nomad service discovery framework"
14-
NAME=nomad
15-
DAEMON=/usr/local/bin/$NAME
16-
PIDFILE=/var/run/$NAME/$NAME.pid
17-
DAEMON_ARGS="agent -server -config {{ nomad_config_dir }}/base.hcl -config {{ nomad_config_dir }}/{{ nomad_node_role }}.hcl ${nomad_FLAGS} >> /var/log/nomad/nomad.log 2>&1"
18-
USER={{ nomad_user }}
19-
SCRIPTNAME=/etc/init.d/$NAME
16+
PATH="{{ nomad_bin_dir }}:/usr/sbin:/usr/bin:/sbin:/bin"
17+
DESC="Nomad service discovery framework"
18+
NAME="nomad"
19+
DAEMON="{{ nomad_bin_dir }}/${NAME}"
20+
PIDFILE="{{ nomad_run_dir }}/${NAME}.pid"
21+
DAEMON_ARGS="agent -server -config {{ nomad_config_dir }}/base.hcl -config {{ nomad_config_dir }}/{{ nomad_node_role }}.hcl >> {{ nomad_log_dir }}/$NAME.log 2>&1"
22+
USER="{{ nomad_user }}"
23+
SCRIPTNAME="/etc/init.d/${NAME}"
2024

21-
[ -x "$DAEMON" ] || exit 0
25+
[ -x "${DAEMON}" ] || exit 0
2226

23-
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
27+
[ -r /etc/default/"${NAME}" ] && . /etc/default/"${NAME}"
2428

2529
[ -f /etc/default/rcS ] && . /etc/default/rcS
2630

2731
. /lib/lsb/init-functions
2832

2933
mkrundir() {
30-
[ ! -d /var/run/nomad ] && mkdir -p /var/run/nomad
31-
chown $USER /var/run/nomad
34+
[ ! -d "{{ nomad_run_dir }}" ] && mkdir -p "{{ nomad_run_dir }}"
35+
chown "${USER}" "{{ nomad_run_dir }}"
3236
}
3337

3438
do_start() {
35-
mkrundir
36-
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chuid $USER --background --make-pidfile --test > /dev/null \
37-
|| return 1
38-
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --chuid $USER --background --make-pidfile -- \
39-
$DAEMON_ARGS \
40-
|| return 2
39+
mkrundir
40+
start-stop-daemon --start --quiet --pidfile "${PIDFILE}" --exec "${DAEMON}" \
41+
--chuid "${USER}" --background --make-pidfile --test > /dev/null \
42+
|| return 1
43+
start-stop-daemon --start --quiet --pidfile "${PIDFILE}" --exec "${DAEMON}" \
44+
--chuid "${USER}" --background --make-pidfile -- \
45+
"${DAEMON_ARGS}" \
46+
|| return 2
4147

42-
for i in `seq 1 30`; do
43-
if ! start-stop-daemon --quiet --stop --test --pidfile $PIDFILE --exec $DAEMON --user $USER; then
44-
RETVAL=2
45-
sleep 1
46-
continue
47-
fi
48-
if "$DAEMON" info >/dev/null; then
49-
return 0
50-
fi
51-
done
52-
return "$RETVAL"
48+
for i in $(seq 1 30); do
49+
if ! start-stop-daemon --quiet --stop --test --pidfile "${PIDFILE}" \
50+
--exec "${DAEMON}" --user "${USER}"; then
51+
RETVAL=2
52+
sleep 1
53+
continue
54+
fi
55+
if "${DAEMON}" info >/dev/null; then
56+
return 0
57+
fi
58+
done
59+
return "${RETVAL}"
5360
}
5461

5562
do_stop() {
56-
if ("${DAEMON}" info 2>/dev/null | grep -q 'server = false' 2>/dev/null) ; then
57-
"$DAEMON" leave
58-
fi
59-
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
60-
RETVAL="$?"
61-
[ "$RETVAL" = 2 ] && return 2
62-
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
63-
[ "$?" = 2 ] && return 2
64-
rm -f $PIDFILE
65-
return "$RETVAL"
63+
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 \
64+
--pidfile "${PIDFILE}" \
65+
--name "${NAME}"
66+
RETVAL="$?"
67+
[ "${RETVAL}" = 2 ] && return 2
68+
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 \
69+
--exec "${DAEMON}"
70+
[ "$?" = 2 ] && return 2
71+
rm -f "${PIDFILE}"
72+
return "${RETVAL}"
6673
}
6774

6875
do_reload() {
69-
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
70-
return 0
76+
start-stop-daemon --stop --signal 1 --quiet --pidfile "${PIDFILE}" \
77+
--name "${NAME}"
78+
return 0
7179
}
7280

7381
case "$1" in
7482
start)
75-
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
83+
[ "${VERBOSE}" != no ] && log_daemon_msg "Starting ${DESC}" "${NAME}"
7684
do_start
7785
case "$?" in
78-
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
79-
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
86+
0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
87+
2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
8088
esac
8189
;;
8290
stop)
83-
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
91+
[ "${VERBOSE}" != no ] && log_daemon_msg "Stopping ${DESC}" "${NAME}"
8492
do_stop
8593
case "$?" in
86-
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
87-
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
94+
0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
95+
2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
8896
esac
8997
;;
90-
log_daemon_msg "Restarting $DESC" "$NAME"
98+
restart|force-reload)
99+
log_daemon_msg "Restarting ${DESC}" "${NAME}"
91100
do_stop
92101
case "$?" in
93102
0|1)
94103
do_start
95104
case "$?" in
96-
0) log_end_msg 0
97-
;;
98-
1) log_end_msg 1
99-
;;
100-
*) log_end_msg 1
101-
;;
105+
0) log_end_msg 0 ;;
106+
1) log_end_msg 1 ;;
107+
*) log_end_msg 1 ;;
102108
esac
103109
;;
104110
*)
111+
# Stop failed
105112
log_end_msg 1
106113
;;
107114
esac
108115
;;
109-
status)
110-
status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
111-
;;
112116
*)
113-
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
117+
echo "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
114118
exit 3
115119
;;
116120
esac
121+
117122
:

templates/nomad_systemd.service.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ After=basic.target network.target
1717
User={{ nomad_user }}
1818
Group={{ nomad_group }}
1919
{% if nomad_node_role == "server" %}
20-
ExecStart=/usr/local/bin/nomad agent -server \
20+
ExecStart={{ nomad_bin_dir }}/nomad agent -server \
2121
-config={{ nomad_config_dir }}/base.hcl \
2222
-config={{ nomad_config_dir }}/{{ nomad_node_role }}.hcl
2323
{% else %}
24-
ExecStart=/usr/local/bin/nomad agent \
24+
ExecStart={{ nomad_bin_dir }}/nomad agent \
2525
-config={{ nomad_config_dir }}/base.hcl \
2626
-config={{ nomad_config_dir }}/{{ nomad_node_role }}.hcl
2727
{% endif %}

0 commit comments

Comments
 (0)