forked from swarmlet/swarmlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·174 lines (149 loc) · 6.26 KB
/
install
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
#!/usr/bin/env bash
# shellcheck disable=SC2162,SC2001,SC2139
FPREFIX="=====>"
PREFIX="----->"
INDENT=" "
export SWARMLET_REPO=${SWARMLET_REPO:="https://github.com/swarmlet/swarmlet.git"}
export SWARMLET_INSTALL_ROOT="/tmp/swarmlet"
# Check if user is root
if [[ $EUID -ne 0 ]]; then
if [[ $(dpkg-query -s sudo) ]]; then
echo "$PREFIX User is not root, using sudo"
export SUDO="sudo"
else
echo "$PREFIX [ERROR] Please install sudo or run installer as root" && exit 1
fi
fi
# Check if Bash is up to date
if [ "${BASH_VERSINFO:-0}" -lt 4 ]; then
echo "$PREFIX [ERROR] Unmet requirements: Bash 4"
echo "$INDENT Your Bash version is $BASH_VERSION"
echo "$PREFIX Exiting with error code 1" 1>&2
exit 1
fi
# Define package managers
declare -A PKG_MANAGERS=(
["/etc/redhat-release"]=yum
["/etc/arch-release"]=pacman
["/etc/gentoo-release"]=emerge
["/etc/SuSE-release"]=zypp
["/etc/debian_version"]=apt-get
)
# Select correct package manager
for KEY in "${!PKG_MANAGERS[@]}"; do
[[ -f $KEY ]] && export PKG_MANAGER="${PKG_MANAGERS[$KEY]}"
done
# Define default environment variables
OPTS_CONFIRMED="true"
declare -A DEFAULT_OPTS=(
[INSTALLATION_TYPE]="interactive" # (default interactive, options: interactive|noninteractive) Use CLI wizard to setup Swarmlet
[INSTALL_FROM]="git" # (default git, options: git|local) The install mode
[SWARMLET_REPO]="$SWARMLET_REPO" # (default swarmlet github url) The default repo to install
[SWARMLET_INSTALL_ROOT]="$SWARMLET_INSTALL_ROOT" # (default swarmlet install root) The default repo to install
[INSTALL_BRANCH]="master" # (default master) The default branch to install
[SWARMLET_USERNAME]="$USER" # (default $USER) Used for authentication with the registry and web services / dashboards
[SWARMLET_PASSWORD]="swarmlet" # (default swarmlet) Used for authentication with the registry and web services / dashboards
[SSH_AUTHORIZED_KEYS]="$HOME/.ssh/authorized_keys" # (default root) The authorized SSH keys for git deployments
[NEW_HOSTNAME]="$HOSTNAME" # (default $HOSTNAME) Optional: set a new hostname
[ROOT_DOMAIN]="" # (default undefined) The domain to use for deployment of included services
[CREATE_SWAP]="false" # (default false) Allocate 1GB of swap space
[INSTALL_ZSH]="false" # (default false) Install 'Oh My Zsh'
[INSTALL_MODULES]="" # (default undefined, options: matomo|swarmpit|swarmprom|portainer) Seperate by space and wrap in quotes to install multiple modules
[CA_SERVER]="production" # (default production, options: production|staging) The Let's Encrypt server to use
[DEBUG]="" # (default undefined) TODO: Run installation in debug mode
)
# Set default environment variables
for KEY in "${!DEFAULT_OPTS[@]}"; do
[[ -n "${DEFAULT_OPTS[$KEY]}" ]] && export "$KEY=${DEFAULT_OPTS[$KEY]}"
done
# Set environment variables from arguments if given
shopt -s extglob
while [[ "$#" -gt 0 ]]; do
case "${1%=*}" in
@($(echo "${!DEFAULT_OPTS[@]}" | sed 's/ /|/g')))
export "${1?}"
shift
;;
*)
echo "Unknown argument: $1"
OPTS_CONFIRMED="false"
shift
;;
esac
done
if [[ $OPTS_CONFIRMED == "false" ]]; then
echo "$PREFIX Current environment:"
for KEY in "${!DEFAULT_OPTS[@]}"; do printenv | grep "$KEY"; done
[[ $INSTALLLATION_TYPE == "noninteractive" ]] && echo "$PREFIX Exiting" && exit 1
read -p "$PREFIX Press enter to continue with this configuration" </dev/tty
fi
set -eo pipefail
[[ $DEBUG ]] && set -x
install() {
echo "$FPREFIX Installing Swarmlet"
echo "$PREFIX Installing required packages"
$SUDO "$PKG_MANAGER" update -y -qq
$SUDO "$PKG_MANAGER" install -y -qq git apt-transport-https whiptail rsync
if [[ "$INSTALL_FROM" = "git" ]]; then
echo "$PREFIX Cloning $SWARMLET_REPO"
if [[ -d $SWARMLET_INSTALL_ROOT && $(command -v swarmlet) ]]; then
echo "$PREFIX Swarmlet is already installed, updating"
echo "$PREFIX Swarmlet is already installed, updating"
else
git clone -q $SWARMLET_REPO $SWARMLET_INSTALL_ROOT
fi
if [[ "$INSTALL_BRANCH" != "master" ]]; then
echo "$PREFIX Checking out $INSTALL_BRANCH"
pushd $SWARMLET_INSTALL_ROOT
git checkout "$INSTALL_BRANCH"
popd
fi
fi
echo "$PREFIX Initializing Swarmlet"
# Preserve SWARMLET_INSTALL_ROOT otherwise swarmlet script will fallback on /mnt/gfs/swarmlet to run the installer
# Preserve SSH_AUTHORIZED_KEYS as we need it to copy SSH public keys
# Preserve SWARMLET_USERNAME and SWARMLET_PASSWORD as we need them to init local registry
# Preserve some other environment variables because we need them too :)
if [[ "$SUDO" = "sudo" ]]; then
SUDO_PARAMS=""
PRESERVE_ENV="
SWARMLET_INSTALL_ROOT
SSH_AUTHORIZED_KEYS
SWARMLET_USERNAME
SWARMLET_PASSWORD
NEW_HOSTNAME
ROOT_DOMAIN
CREATE_SWAP
INSTALL_ZSH
INSTALL_MODULES
CA_SERVER
DEBUG
"
for env_var in ${PRESERVE_ENV}; do
SUDO_PARAMS="${SUDO_PARAMS} ${env_var}=${!env_var}"
done
# A more radical way is to keep all environment variables
# But this seems a bit risky and not under control...
# SUDO_PARAMS="-E"
else
SUDO_PARAMS=""
fi
$SUDO ${SUDO_PARAMS} "$SWARMLET_INSTALL_ROOT/swarmlet" init
unset SWARMLET_INSTALL_ROOT
echo "$PREFIX Verifying installation"
[[ $($SUDO bash -c "command -v swarmlet") ]] || exit
echo "$PREFIX Installation verified"
echo "$FPREFIX Server initialized as manager node"
$SUDO swarmlet node ls
echo "$FPREFIX Swarmlet modules deployed"
$SUDO swarmlet stack ls
echo "$FPREFIX Installation complete"
echo "$PREFIX Check the docs on how to configure a domain name for Swarmlet services"
echo "$INDENT https://swarmlet.dev/docs/getting-started/introduction"
echo "$PREFIX Please wait a minute or two for Traefik to initialize..."
echo "$PREFIX Follow the logs using:"
echo "$INDENT $ docker service logs router_traefik -f"
echo "$PREFIX Check status of services using:"
echo "$INDENT $ docker service ls"
}
install "$@"