#!/usr/bin/env bash
# Install salt
# ascii/stretch version  2016.11.2+ds-1
# ceres version          2017.7.1+ds-1
# saltstack repo version 2017.7.1+ds-1


# Debian 11 (Bullseye):


# sudo curl -fsSL -o /etc/apt/keyrings/salt-archive-keyring-2023.gpg https://repo.saltproject.io/salt/py3/debian/11/amd64/SALT-PROJECT-GPG-PUBKEY-2023.gpg
# echo "deb [signed-by=/etc/apt/keyrings/salt-archive-keyring-2023.gpg arch=amd64] https://repo.saltproject.io/salt/py3/debian/11/amd64/latest bullseye main" | sudo tee /etc/apt/sources.list.d/salt.list

#DEBIAN_VERSION=8
#DEBIAN_RELEASE=jessie

DEBIAN_VERSION=11
DEBIAN_RELEASE=bullseye

ARCH="amd64"

# DEBIAN_VERSION=12
# DEBIAN_RELEASE=bookworm

SALT_VERSION=latest 

REFRESHED_AT=2023-11-06

mkdir -p /etc/apt/sources.list-available

# KEY_DIR="/etc/apt/trusted.gpg.d"  # if you don't want to has signed-by in the sources.list
KEY_DIR="/etc/apt/keyrings"

mkdir -p ${KEY_DIR}

sudo curl -fsSL -o ${KEY_DIR}/salt-archive-keyring.gpg https://repo.saltproject.io/py3/debian/${DEBIAN_VERSION}/${ARCH}/latest/salt-archive-keyring.gpg

# NB Packages now have dh-systemd deps and --with-systemd treat as debian upstream
echo "#sudo curl -fsSL -o ${KEY_DIR}/salt-archive-keyring.gpg https://repo.saltproject.io/py3/debian/${DEBIAN_VERSION}/${ARCH}/latest/salt-archive-keyring.gpg"  | sudo tee /etc/apt/sources.list.d/salt.list

echo "deb [signed-by=${KEY_DIR}/salt-archive-keyring.gpg arch=${ARCH}] https://repo.saltproject.io/py3/debian/${DEBIAN_VERSION}/${ARCH}/latest ${DEBIAN_RELEASE} main" | sudo tee /etc/apt/sources.list.d/salt.list
#ln -s /etc/apt/sources.list-available/saltstack-${DEBIAN_RELEASE}.list /etc/apt/sources.list.d/saltstack-${DEBIAN_RELEASE}.list

# Upgrade The System
apt update && apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade -y


# salt-api 			- Generic, modular network access system
# salt-cloud 			- public cloud VM management system
# salt-common 			- shared libraries that salt requires for all packages
# salt-doc 			- additional documentation for salt, the distributed remote execution system
# salt-master 			- remote manager to administer servers via salt
# salt-minion 			- client package for salt, the distributed remote execution system
# salt-proxy 			- Proxy client package for salt stack
# salt-ssh 			- remote manager to administer servers via Salt SSH
# salt-syndic 			- master-of-masters for salt, the distributed remote execution system
# salt-formula-kubernetes 	- Salt formula for Kubernetes
# salt-formula-ceilometer 	- Salt formula for OpenStack Ceilometer
# salt-formula-cinder 		- Salt formula for OpenStack Cinder
# salt-formula-glance 		- Salt formula for OpenStack Glance
# salt-formula-heat 		- Salt formula for OpenStack Heat
# salt-formula-horizon 		- Salt formula for OpenStack Horizon
# salt-formula-keystone 	- Salt formula for OpenStack Keystone
# salt-formula-neutron 		- Salt formula for OpenStack Neutron
# salt-formula-nova 		- Salt formula for OpenStack Nova
# salt-formula-swift 		- Salt formula for OpenStack Swift


# Install Salt Dependencies
apt install -y -o "Dpkg::Options::=--force-confold" \
	python \
	apt-utils \
	software-properties-common \
	python-yaml \
	python-m2crypto \
	python-crypto \
	python-msgpack \
	python-zmq \
	python2.7-doc \
	python-jinja2 \
	python-requests 

apt install -y salt-minion

# Salt-master
apt install -yq -o -o "Dpkg::Options::=--force-confold" --no-install-recommends \
	salt-master \
	salt-api \
	salt-ssh \
	salt-syndic \
	salt-cloud \
	reclass \
	python-apt \
	python-git \
	python-openssl \
	python-cherrypy3 \
	git \
	openssh-client \
	make

# Or get the development version
# apt upgrade -y -o "Dpkg::Options::=--force-confold" && \
# apt install -y -o "Dpkg::Options::=--force-confold" curl
# curl -L https://bootstrap.saltstack.com | sh -s -- -X git develop

# salt-common in debian is missing zsh completions
# source: http://github.com/saltstack/salt/tree/develop/pkg/ *.{bash,zsh} 
# FYI: completition provided by salt-common pkg 

test -e /usr/share/bash-completion/completions/salt-common || \
	curl -sL "https://raw.githubusercontent.com/saltstack/salt/develop/pkg/salt.bash" | tee /usr/share/bash-completion/completions/salt-common 

test -e /usr/share/zsh/vendor-completions/_salt || \
	curl -sL "https://raw.githubusercontent.com/saltstack/salt/develop/pkg/zsh_completion.zsh" | tee /usr/share/zsh/vendor-completions/_salt 
