forked from swarmlet/swarmlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·143 lines (122 loc) · 5.12 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
#!/usr/bin/env bash
# shellcheck disable=SC2162,SC2001
FPREFIX="=====>"
PREFIX="----->"
INDENT=" "
SWARMLET_REPO=${SWARMLET_REPO:="https://github.com/swarmlet/swarmlet.git"}
SWARMLET_INSTALL_ROOT="/opt/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 default environment variables
ENV_CONFIRMED="true"
declare -A DEFAULT_OPTS=(
[INSTALLATION_TYPE]=interactive # (default interactive, options: interactive|noninteractive) Use CLI wizard to setup Swarmlet
[SWARMLET_REPO]=$SWARMLET_REPO # (default swarmlet github url) 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
# > TODO:
# [DEBUG]= # (default undefined) 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"
ENV_CONFIRMED="false"
shift
;;
esac
done
if [[ $ENV_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
# 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 MANAGER in "${!PKG_MANAGERS[@]}"; do
[[ -f $MANAGER ]] && PKG_MANAGER="${PKG_MANAGERS[$MANAGER]}"
done
set -eo pipefail
[[ $DEBUG ]] && set -x
install() {
echo "$FPREFIX Installing Swarmlet"
echo "$PREFIX Installing required packages"
# TODO: support other package managers
$SUDO "$PKG_MANAGER" update -y -qq &>/dev/null
$SUDO "$PKG_MANAGER" install -y -qq apt-transport-https &>/dev/null
[[ $(command -v git) ]] || $SUDO "$PKG_MANAGER" install -y -qq git
echo "$PREFIX Cloning $SWARMLET_REPO"
if [[ -d $SWARMLET_INSTALL_ROOT && $(command -v swarmlet) ]]; then
echo "$PREFIX Swarmlet is already installed, updating"
# TODO:
# pushd $SWARMLET_INSTALL_ROOT >/dev/null
# git pull origin "$INSTALL_BRANCH"
# popd >/dev/null
# swarmlet update
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 >/dev/null
git checkout "$INSTALL_BRANCH"
popd >/dev/null
fi
echo "$PREFIX Linking executable"
[[ $(command -v swarmlet) ]] || ln -s $SWARMLET_INSTALL_ROOT/swarmlet /usr/local/sbin/
echo "$PREFIX Initializing Swarmlet"
swarmlet init
echo "$FPREFIX Server initialized as manager node"
swarmlet node ls
echo "$FPREFIX Swarmlet modules deployed"
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 "$@"