ec2_post_init
Populate STScI EC2 instances with ease
miniconda.inc.sh
Go to the documentation of this file.
1## @file
2## @brief Miniconda control functions
3## @details
4## @section miniconda_example Example
5## @include miniconda_setup.sh
6
7(( $EC2PINIT_MINICONDA_INCLUDED )) && return
8EC2PINIT_MINICONDA_INCLUDED=1
9source ec2pinit.inc.sh
10
11# URL to a site providing miniconda installers
12mc_url="https://repo.anaconda.com/miniconda"
13
14# Name of miniconda installation script
15mc_installer="miniconda3_install.sh"
16
17
18## @fn _get_rc()
19## @private
20## @brief Get the default bash rc script for the user account
21## @code{.sh}
22## # Red Hat...
23## rc=$(_get_rc)
24## # rc=/home/example/.bash_profile
25##
26## Debian...
27## rc=$(_get_rc)
28## # rc=/home/example/.bashrc
29## @endcode
30## @retval 1 if ``home`` does not exist
32 local scripts=(.bashrc .bashrc_profile .profile)
33 local home="$(sys_user_home ${1:-$USER})"
34 if [ -z "$home" ] || [ ! -d "$home" ]; then
35 return 1
36 fi
37
38 for x in "${scripts[@]}"; do
39 local filename="$home/$x"
40 [ ! -f "$filename" ] && continue
41 echo $filename
42 break
43 done
44}
45
46
47## @fn mc_get()
48## @brief Download Miniconda3
49## @details Installation script destination is set by global $mc_installer
50## @param version Miniconda3 release version...
51## (i.e., py39_4.11.0)
52## @param version "latest" if empty
53## @see config.sh
55 local version="${1:-latest}"
56 local dest="$ec2pinit_tempdir"
57 local platform="$(sys_platform)"
58 local arch="$(sys_arch)"
59 local name="Miniconda3-$version-$platform-$arch.sh"
60
61 if [ -f "$dest/$mc_installer" ]; then
62 io_warn "mc_get: $dest/$mc_installer exists"
63 return 1
64 fi
65 io_info "mc_get: Downloading $mc_url/$name"
66 io_info "mc_get: Destination: $dest/$mc_installer"
67
68 curl -L -o "$dest/$mc_installer" "$mc_url/$name"
69 # Is this a bug or what?
70 # curl exits zero on success but causes "if mc_get" to fail
71 # as if it exited non-zero. I'm forcing the conditions I need
72 # below.
73 (( $? != 0 )) && return 1
74 return 0
75}
76
77
78## @fn mc_configure_defaults()
79## @brief Sets global defaults for conda and pip
81 if [ -z "$CONDA_EXE" ]; then
82 # Not initialized correctly
83 io_error "mc_configure_defaults: conda is not initialized"
84 return 1
85 fi
86 io_info "mc_configure_defaults: Configuring conda options"
87 conda config --system --set auto_update_conda false
88 conda config --system --set always_yes true
89 conda config --system --set report_errors false
90
91 # Some skeletons default to .bashrc instead of .bash_profile.
92 local rc="$(_get_rc)"
93 io_info "mc_configure_defaults: Enabling verbose output from pip"
94 if ! grep -E '[^#](export)?[\t\ ]+PIP_VERBOSE=' "$rc" &>/dev/null; then
95 echo export PIP_VERBOSE=1 >> "$rc"
96 io_info "mc_configure_defaults: $rc modified"
97 else
98 io_info "mc_configure_defaults: $rc not modified"
99 fi
100}
101
102
103## @fn mc_initialize()
104## @brief Configures user account to load conda at login
105## @param dest path to miniconda installation root
107 local dest="$1"
108 if [[ $- =~ v ]]; then
109 set +v
110 trap 'set -v' RETURN
111 fi
112 if [[ $- =~ x ]]; then
113 set +v
114 trap 'set -x' RETURN
115 fi
116
117 io_info "mc_initialize: Using conda: $dest"
118
119 # Configure the user's shell
120 if (( ! $EUID )); then
121 if ! grep -E '#.*>>>.*conda initialize.*>>>$' $(_get_rc) &>/dev/null; then
122 sudo -u $USER dest=$dest -i bash -c 'source "$dest"/etc/profile.d/conda.sh ; conda init'
123 fi
124 fi
125
126 # Give current context access to the miniconda installation
127 source "$dest"/etc/profile.d/conda.sh
129}
130
131
132## @fn mc_install()
133## @brief Installs Miniconda3
134## @param version of the Miniconda3 installer (i.e., py39_4.11.0)
135## @param dest path to install Miniconda3 (~/miniconda3)
136## @retval 1 if any argument is invalid
137## @retval 1 if destination exists
138## @retval 1 if download fails
139## @retval 1 if installation fails (implicit)
141 local version="$1"
142 local dest="$2"
143 local cmd="bash "$ec2pinit_tempdir/$mc_installer" -b -p $dest"
144
145 if [ -z "$version" ]; then
146 io_error "mc_install: miniconda version required" >&2
147 return 1
148 fi
149
150 if [ -z "$dest" ]; then
151 io_error "mc_install: miniconda destination directory required" >&2
152 return 1
153 elif [ -d "$dest" ]; then
154 io_error "mc_install: miniconda destination directory exists" >&2
155 return 1
156 fi
157
158 if mc_get "$version"; then
159 io_error "mc_install: unable to obtain miniconda from server" >&2
160 return 1
161 fi
162
163 io_info "mc_install: Installing conda: $dest"
164 $cmd
165}
166
167
168## @fn mc_clean()
169## @brief Remove unused tarballs, caches, indexes, etc
170## @retval 1 if miniconda is not initialized
172 if [ -z "$CONDA_EXE" ]; then
173 # Not initialized correctly
174 io_error "mc_clean: conda is not initialized"
175 return 1
176 fi
177
178 conda clean --all
179}
180
io_warn(...)
Print a warning message.
Definition: io.inc.sh:44
io_info(...)
Print a message.
Definition: io.inc.sh:29
io_error(...)
Print an error message.
Definition: io.inc.sh:59
mc_initialize(dest)
Configures user account to load conda at login.
mc_configure_defaults()
Sets global defaults for conda and pip.
mc_get(version, version)
Download Miniconda3.
mc_install(version, dest)
Installs Miniconda3.
_get_rc()
Get the default bash rc script for the user account.
mc_clean()
Remove unused tarballs, caches, indexes, etc.