-
Notifications
You must be signed in to change notification settings - Fork 0
/
envmgr
executable file
·207 lines (195 loc) · 6.73 KB
/
envmgr
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
# Simple shell script for symlinking all files from .rc dir to home directory
# Also upgrades, cleanups homebrew and similar stuff in single command
# Actually absurdly ugly, but hey, it works! Somehow.
#colors
COLOR_RESET="$(tput sgr0)"
COLOR_GREEN="$(tput setaf 2)"
#source config
source ~/.rc/envmgr.conf
# Function to symlink dotfiles
# Checking if OS is Linux (GNU coreutils), if not, we assume OS uses BSD utils
symlink() {
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
ln -sfvn ${1} ${2}
else
ln -sfvh ${1} ${2}
fi
}
#Symlink dotfiles and envmgr from ~/.rc to ~
install_env() {
echo -e "${COLOR_GREEN}Deploying environment${COLOR_RESET}"
cd ~/.rc || exit
echo -e "${COLOR_GREEN}Deploying dotfiles from $(pwd) to ${HOME}${COLOR_RESET}"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
dotdirs=( .config .local/share )
for dir in "${dotdirs[@]}"; do
if [[ ! -d ~/${dir} ]]; then
mkdir -p ~/${dir}
fi
done
dotfiles=("${dotfiles[@]}" "${linuxenv[@]}")
fi
for file in "${dotfiles[@]}"; do
symlink ~/.rc/.${file} ~/.${file}
done
symlink ~/.rc/envmgr ~/.rc/.bin/envmgr
cd - >&/dev/null || exit 1
echo -e "${COLOR_GREEN}Finished deploying dotfiles${COLOR_RESET}"
echo -e "${COLOR_GREEN}Installing vim plugins${COLOR_RESET}"
vim +PlugInstall +qall
echo -e "${COLOR_GREEN}Finished installing vim plugins${COLOR_RESET}"
}
# Install asdf and plugins (ruby, python, nodejs, golang)
# Plugins listed in envmgr.conf in asdf_plugins array
# TODO: add cleanup of old versions, maybe as separate func
install_devenv() {
#install asdf
echo -e "${COLOR_GREEN}Installing asdf${COLOR_RESET}"
if [[ ! -d "$HOME/.asdf" ]]; then
git clone https:/asdf-vm/asdf.git ~/.asdf
else
cd ~/.asdf && git pull
fi
#init asdf
source ~/.rc/sh.d/S01_env
#install plugins
echo -e "${COLOR_GREEN}Installing asdf plugins${COLOR_RESET}"
for lang in "${asdf_plugins[@]}"; do
asdf plugin-add ${lang}
done
#setup default packages
for gem in "${gems[@]}"; do
grep -sq ${gem} ~/.default-gems || echo ${gem} >> ~/.default-gems
done
for egg in "${eggs[@]}"; do
grep -sq ${egg} ~/.default-python-packages || echo ${egg} >> ~/.default-python-packages
done
for npm_package in "${npms[@]}"; do
grep -sq ${npm_package} ~/.default-npm-packages || echo ${npm_package} >> ~/.default-npm-packages
done
for go_pkg in "${gopkgs[@]}"; do
grep -sq ${go_pkg} ~/.default-golang-pkgs || echo ${go_pkg} >> ~/.default-golang-pkgs
done
#install latest versions of langs
#nodejs verifies signatures, assuming trusted environment
#FIXME: support GPG verification for nodejs and python
echo -e "${COLOR_GREEN}Installing languages with asdf${COLOR_RESET}"
for lang in "${asdf_plugins[@]}"; do
latest_version=$(asdf list-all ${lang} | sort -V | grep -v '[a-z]' | tail -1)
echo "Latest ${lang} => ${latest_version}"
NODEJS_CHECK_SIGNATURES=no asdf install ${lang} ${latest_version}
asdf global ${lang} ${latest_version}
done
echo -e "${COLOR_GREEN}Finished installing asdf${COLOR_RESET}"
}
#Installs homebrew on os x (global, script) and linux (manual, homedir)
#Also installs apps from ~/.rc/Brewfile
install_homebrew() {
cd ~/.rc || exit
echo -e "${COLOR_GREEN}Installing homebrew and favourite packages${COLOR_RESET}"
if hash brew 2>/dev/null; then
echo "Brew is already installed!"
else
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
git clone https:/Homebrew/brew ~/.linuxbrew/Homebrew
mkdir ~/.linuxbrew/bin
ln -s ~/.linuxbrew/Homebrew/bin/brew ~/.linuxbrew/bin
eval $(~/.linuxbrew/bin/brew shellenv)
elif [[ "$OSTYPE" == "darwin"* ]]; then
#probably i should migrate to manual install.
#Running (even respected) scripts from interwebs is stupid.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
fi
brew bundle
}
install_kubectl_plugins() {
cd ~/.rc || exit
echo -e "${COLOR_GREEN}Installing kubectl plugins${COLOR_RESET}"
if hash kubectl-krew 2>/dev/null; then
kubectl krew install "${kubectl_plugins}"
fi
}
install_helm_plugins() {
cd ~/.rc || exit
echo -e "${COLOR_GREEN}Installing helm plugins${COLOR_RESET}"
if hash helm 2>/dev/null; then
helm plugin install "${helm_plugins}"
fi
}
#Nukes whole dotfiles setup
#TODO: Maybe use shred instead of rm?
env_nuke() {
echo "Cleaning up env"
echo "Removing dotfiles symlinks"
for file in "${dotfiles[@]}"; do
rm -vf ~/.${file}
done
echo "Cleaning up devenv"
rm -rvf ~/.asdf
echo "Removing history"
rm -vf ~/.zsh-history
rm -vf ~/.bash_history
echo "Removing linuxbrew"
rm -rf ~/.linuxbrew
echo "Removing dotfiles directory"
rm -rf ~/.rc
}
#Checks for updates in env, homebrew, vim plugins and mac app store
env_housekeeper() {
echo -e "${COLOR_GREEN}Started housekeeping${COLOR_RESET}"
echo -e "${COLOR_GREEN}Updating dotfiles repo${COLOR_RESET}"
git -C ~/.rc pull
echo -e "${COLOR_GREEN}Cleaning up outdated symlinks${COLOR_RESET}"
find -L $HOME -maxdepth 1 -type l -exec rm -i {} +
if hash brew 2>/dev/null; then
echo -e "${COLOR_GREEN}Updating and cleaning up homebrew${COLOR_RESET}"
brew update
brew upgrade
brew cleanup
fi
# Update asdf
echo -e "${COLOR_GREEN}Updating asdf and asdf plugins${COLOR_RESET}"
if hash asdf 2>/dev/null; then
asdf update
asdf plugin-update --all
fi
if hash kubectl-krew 2>/dev/null; then
kubectl krew upgrade
fi
# helm doesn't have any checks for versions, so will always run. Disable for now
# if hash helm 2>/dev/null; then
# helm_installed_plugins=$(helm plugin list | tail -n +2 | awk '{print $1}')
# for plugin in $helm_installed_plugins; do
# helm plugin update $plugin
# done
# fi
if hash mas 2>/dev/null; then
echo -e "${COLOR_GREEN}Updating mac app store apps${COLOR_RESET}"
mas upgrade
fi
if hash vim 2>/dev/null; then
echo -e "${COLOR_GREEN}Updating vim plugins${COLOR_RESET}"
vim +PlugUpdate +PlugUpgrade +PlugClean +qall
fi
echo -e "${COLOR_GREEN}Housekeeping finished${COLOR_RESET}"
}
#Arguments parsing
case "${1}" in
env) install_env ;;
devenv) install_devenv ;;
brew) install_homebrew ;;
fullenv) install_env && install_devenv && install_homebrew && install_kubectl_plugins && install_helm_plugins ;;
nuke) env_nuke ;;
maid) env_housekeeper ;;
*)
echo "Command list:"
echo "env - symlink dotfiles"
echo "devenv - install asdf with plugins"
echo "brew - install homebrew and packages from bundle"
echo "fullenv - env, devenv and brew combined"
echo "nuke - remove (almost) everything installed by envmgr"
echo "maid - upgrade stuff installed by envmgr"
esac