Skip to content

Commit

Permalink
Use new deployment model
Browse files Browse the repository at this point in the history
  • Loading branch information
sabandi committed Aug 25, 2021
1 parent d37b35b commit d3c7c84
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 51 deletions.
3 changes: 2 additions & 1 deletion defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ con_ports_list_all: "{{ con_ports_list_extras + con_ports_list_default }}"
con_ports_list_string: "{% for port in con_ports_list_all|reject('match', '^$') %} -p {{ port }}{% endfor %}"

# !!!! BEWARE: This is to completely override everything passed into the service template for the docker run. DON'T EDIT THIS UNLESS YOU KNOW WHAT YOUR DOING!!!!!
con_docker_run_params: "--name=avicontroller{{ con_ports_list_string }} -d --privileged{{ con_env_variables_string }}{{ con_mounts_string }} {{ con_image }}"
AVI_RUN_PARAMS: "--name=avicontroller{{ con_ports_list_string }} -d --privileged{{ con_env_variables_string }}{{ con_mounts_string }} {{ con_image }}"
AVI_EXECUTABLE: "/usr/sbin/avicontroller"
8 changes: 5 additions & 3 deletions tasks/docker/services/install.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
---
- name: Avi Controller | Services | systemd | Install the Avi Controller service
include: "systemd/install.yml"
when: ansible_service_mgr == "systemd"
- name: Avi Controller | Services | Install the Avi Controller service file | New
template: src=avicontroller.j2 dest=/usr/sbin/avicontroller mode=0755
notify: Avi Controller | Services | Restart the avicontroller service

- name: Avi Controller | Services | {{ ansible_service_mgr }} | Install the Avi Controller service | New
include: "{{ ansible_service_mgr }}/install.yml"

- name: Avi Controller | Services | {{ ansible_service_mgr }} | Start the service since it's not running |
service: name=avicontroller enabled=yes state=started
Expand Down
2 changes: 1 addition & 1 deletion tasks/docker/services/systemd/install.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- name: Avi Controller | Services | systemd | Deploy the avicontroller service
template:
src: 16.x/avicontroller.service.j2
src: avicontroller.service.j2
dest: /etc/systemd/system/avicontroller.service
mode: 0644
register: avicontroller_service
Expand Down
124 changes: 78 additions & 46 deletions templates/avicontroller.j2
Original file line number Diff line number Diff line change
@@ -1,104 +1,132 @@
#!/bin/bash
## Usage: avise [options] ARG1

## Usage: avicontroller [options] ARG1
## Description: This script manages and provides manage of the Avi Controller service.
## Author: Eric Anderson <support@avinetworks.com>
## docker is alias to the container platform i.e to either docker or podman

container=avicontroller
logfile="/var/log/${container}.log"

if [ -f /etc/default/avicontroller ]; then
. /etc/default/avicontroller
elif [ -f /etc/sysconfig/avicontroller ]; then
. /etc/sysconfig/avicontroller
else
echo "Failed to start: missing defaults"
exit 1
fi
write_log() {
echo $1 2>&1 | tee -a ${logfile}
}

populate_container_platform()
{
# Default container platform is docker
cont_plat="docker"
cont_plat_sock="/var/run/docker.sock"

# Possible values for $distribution which we support
# "ubuntu" (or) "ol" (or) "rhel" (or) "centos"
distribution=$(grep "^ID=" /etc/os-release | cut -d"=" -f2 | sed 's/"//g')

# Possible known values for $version which we support
# On Ubuntu:
# version = "16.04" (or) "18.04" (or) "20.04"
# On RHEL / OL / CENTOS:
# version = "7.*" (or) "8.*"
version=$(grep "^VERSION_ID=" /etc/os-release | cut -d"=" -f2 | sed 's/"//g')

# cont_plat is podman only on OL / RHEL / CENTOS 8.*
if [[ $distribution == "ol" || $distribution == "centos" || $distribution == "rhel" ]]; then
if [[ $version =~ 8.* ]]; then
cont_plat="podman"
cont_plat_sock="/run/podman/podman.sock"
fi
fi
write_log "[`date`] Setting cont_plat to $cont_plat as distribution=$distribution ($version)"
}

write_log() {
echo $1 2>&1 | tee -a ${AVICONTROLLER_LOGFILE}
wait_till_service_starts()
{
service=$1
write_log "[`date`] Waiting to check if ${service}.service is active"
while [[ $(systemctl is-active ${service}.service) != "active" ]]
do
sleep 1
done
write_log "[`date`] ${service}.service is active"
}

container_running() {
/usr/bin/docker ps -f name=avicontroller | grep -q avicontroller
${cont_plat} ps -f name=${container} | grep -q ${container}
}

container_exists() {
/usr/bin/docker ps -a -f name=avicontroller | grep -q avicontroller
${cont_plat} ps -a -f name=${container} | grep -q ${container}
}

start_container() {
write_log "[`date`] Starting the avicontroller container"
/usr/bin/docker run ${AVICONTROLLER_DOCKER_RUN_PARAMS}
write_log "[`date`] Starting the ${container} container"
${cont_plat} run ${controller_docker_run_params}
if [ $? -eq 0 ]; then
write_log "[`date`] Service avicontroller running..."
write_log "[`date`] Service ${container} running..."
else
write_log "[`date`] Failed to start the avicontroller container"
write_log "[`date`] Failed to start the ${container} container"
exit 1
fi
}

stop_container() {
/usr/bin/docker stop -t 60 avicontroller
${cont_plat} stop ${container}
if [ $? -eq 0 ]; then
write_log "[`date`] Stopped the avicontroller container"
write_log "[`date`] Stopped the ${container} container"
else
exit 1
fi
}

remove_container() {
write_log "[`date`] Removing existing avicontroller container"
/usr/bin/docker rm -f avicontroller
write_log "[`date`] Removing existing ${container} container"
${cont_plat} rm -f ${container}
if [ $? -eq 0 ]; then
write_log "[`date`] Removed existing avicontroller container"
write_log "[`date`] Removed existing ${container} container"
else
write_log "[`date`] Failed to remove existing avicontroller container"
write_log "[`date`] Failed to remove existing ${container} container"
fi
}

prestart() {
# Clean up any running or existing avicontroller containers
write_log "[`date`] Performing avicontroller pre-start step."
if container_running && container_exists; then
write_log "[`date`] Service avicontroller is already running."
elif ! container_running && container_exists; then
write_log "[`date`] Container avicontroller already exists, but not running. This can be caused by an
improper shutdown, or service crash. Performing cleanup"
remove_container
start_container
else
start_container
fi
write_log "[`date`] Performing ${container} pre-start step."
remove_container
start_container
}

start() {
prestart
/usr/bin/docker wait avicontroller
${cont_plat} wait ${container}
}

stop() {
# Stopping the docker process if running
# Stopping the docker/podman process if running
controller_pid=`${cont_plat} inspect $container | grep '\"Pid\"' | awk '{print $2}' | sed s/,//`
stop_var="fstrim /proc/$controller_pid/root"
${stop_var}
write_log "[`date`] Freed the blocks of ${controller_pid}..."
if container_running; then
write_log "[`date`] Service avicontroller stopping..."
write_log "[`date`] Service ${container} stopping..."
stop_container
else
write_log "[`date`] Service avicontroller already stopped"
write_log "[`date`] Service ${container} already stopped"
fi
poststop
}

poststop() {
if $(ip addr | grep -q ${AVICONTROLLER_DEV_NAME}:avivip); then
/bin/bash -c "ip addr del $(ip addr | grep ${AVICONTROLLER_DEV_NAME}:avivip | awk '{print $2}') dev ${AVICONTROLLER_DEV_NAME}"
if $(ip addr | grep -q {{ AVICONTROLLER_DEV_NAME }}:avivip); then
/bin/bash -c "ip addr del $(ip addr | grep {{ AVICONTROLLER_DEV_NAME }}:avivip | awk '{print $2}') dev {{ AVICONTROLLER_DEV_NAME }}"
if [ $? -eq 0 ]; then
write_log "[`date`] Successfuly removed the cluster vip interface."
fi
elif $(ip addr | grep -q ${AVICONTROLLER_DEV_NAME}:1); then
/bin/bash -c "ip addr del $(ip addr | grep ${AVICONTROLLER_DEV_NAME}:1 | awk '{print $2}') dev ${AVICONTROLLER_DEV_NAME}"
elif $(ip addr | grep -q {{ AVICONTROLLER_DEV_NAME }}:1); then
/bin/bash -c "ip addr del $(ip addr | grep {{ AVICONTROLLER_DEV_NAME }}:1 | awk '{print $2}') dev {{ AVICONTROLLER_DEV_NAME }}"
if [ $? -eq 0 ]; then
write_log "[`date`] Successfuly removed the cluster vip interface."
fi
fi

if container_exists; then
remove_container
fi
Expand All @@ -115,19 +143,23 @@ reload() {

status() {
if container_running; then
echo "avicontroller running..."
echo "${container} running..."
else
echo "avicontroller stopped"
echo "${container} stopped"
fi
}


case "$1" in
start|stop|status|restart|reload)
populate_container_platform
wait_till_service_starts ${cont_plat}
controller_docker_run_params="{{ AVI_RUN_PARAMS }}"
$1
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
exit 2
esac

exit $?
exit $?
14 changes: 14 additions & 0 deletions templates/avicontroller.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=AviCONTROLLER
Requires=proc-sys-fs-binfmt_misc.mount


[Service]
TimeoutStartSec=0
Restart=always

ExecStart={{ AVI_EXECUTABLE }} start
ExecStop={{ AVI_EXECUTABLE }} stop

[Install]
WantedBy=multi-user.target

0 comments on commit d3c7c84

Please sign in to comment.