Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scripts/install/auto-install.sh #824

Merged
merged 43 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9917cf4
First draft of an auto-install script for POCS.
jamessynge Apr 14, 2019
d4ebdf8
Fix name of default branch (develop, not master).
jamessynge Apr 14, 2019
8f17431
Add cloning/pulling of PAWS
jamessynge Apr 14, 2019
b5a9f2d
Re-enable calling install-dependencies.sh
jamessynge Apr 14, 2019
7cdc970
Add some more whitespace, so it is easier to parse the output
jamessynge Apr 14, 2019
52dd23a
Add a separator line (echo_bar) between major commands
jamessynge Apr 14, 2019
41cdf85
Remove debugging option
jamessynge Apr 14, 2019
c16558b
Adapt to change in astrometry version string format.
jamessynge Apr 14, 2019
d11f8fe
Correctly punctuate a multi-line pipe
jamessynge Apr 14, 2019
4031a81
Add install-gcloud.sh, which installs Google Cloud SDK
jamessynge Apr 15, 2019
9795b1e
Add additional documentation
jamessynge Apr 16, 2019
9efc869
Add info about how create the PANUSER.
jamessynge May 4, 2019
ef1cd69
Provide more guidance on how to run the script.
jamessynge May 4, 2019
3f60c0d
Add missing fi
jamessynge May 4, 2019
9627824
Fix then
jamessynge May 4, 2019
13c2c91
wip
jamessynge May 4, 2019
452d8bc
fix redirects
jamessynge May 4, 2019
de5a509
wip
jamessynge May 4, 2019
ec2d089
wip
jamessynge May 4, 2019
1abd4e1
wip
jamessynge May 4, 2019
e1ed9cf
Set group owner of /var/panoptes
jamessynge May 4, 2019
32f3095
Try $COLUMNS for width of terminal.
jamessynge May 4, 2019
c4188fa
Try "tput cols" for width of terminal.
jamessynge May 4, 2019
8523041
Add build-essentials dependency
jamessynge May 4, 2019
c5431c6
Fix build-essential dependency
jamessynge May 4, 2019
35e8bdb
Add urllib3 to requirements.txt
jamessynge May 5, 2019
e8cdc37
Restrict version of urllib3
jamessynge May 5, 2019
c84fdda
Support both origin and upstream for POCS clone.
jamessynge May 5, 2019
f452e32
Fix bug in update_shell_rc_file.
jamessynge May 5, 2019
42c290d
Set name of remote during clone.
jamessynge May 5, 2019
1b14b96
Add missing punctuation, fix comments.
jamessynge May 5, 2019
db22f84
Add function ensure_directory_ownership, use for $PANDIR
jamessynge May 5, 2019
a0e2ad2
Add a question for Wilfred about whether to execute
jamessynge May 5, 2019
e2753a1
Change format of an output string so that shellcheck isn't confused b…
jamessynge May 5, 2019
02c3d1e
Add support for conditionally invoking install-gcloud.sh
jamessynge May 5, 2019
8118344
Remove verbose logging from install-apt-packages.sh.
jamessynge May 5, 2019
96350bd
Reformat message from echo_running_sudo.
jamessynge May 5, 2019
7d7b52d
Replace use of curl with wget.
jamessynge May 5, 2019
3955798
Make apt-get quiet.
jamessynge May 5, 2019
433471f
Remove duplicate sources for the gcloud sdk.
jamessynge May 5, 2019
732a2ca
Reformat sudo message.
jamessynge May 5, 2019
0f77dc6
Don't clone POCS or PAWS if it appears they exist; instead issue
jamessynge May 11, 2019
f742e52
Change clean_PATH so it can be standalone.
jamessynge May 12, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ scikit_image >= 0.12.3
scipy >= 0.17.1
transitions >= 0.4.0
tweepy

# requests 2.21 doesn't like urllib3 >=1.25
urllib3 >= 1.24, < 1.25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we could somehow put a timer on this. urllib3 1.25.x is only about two weeks old, so I expect this will resolve itself shortly.

Of course, there are not really any features in 1.25 that we require so should be okay.


wcsaxes
3 changes: 3 additions & 0 deletions scripts/install/apt-packages-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ coreutils
# includes this file).
git

# Installing some POCS dependencies (esp. astroscrappy) require a C compiler,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think astroscrappy is required by ccdproc, which is still in develop but which I think I have removed from, i.e. #279. We don't really use it for POCS (we might for PIAA).

Not terrible to have build-essential here, but if we use this script to build Docker images later we will want to minimize this as it causes bloat.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged.

# for which we install gcc via the build-essentials package.
build-essential
316 changes: 316 additions & 0 deletions scripts/install/auto-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
#!/bin/bash
# auto-install.sh installs git if it isn't installed, then clones POCS and
# installs its dependencies.
#
# To fetch this script from github and execute it immediately,
# run these commands:
#
# export POCS_GITHUB_USER="panoptes"
# export POCS_BRANCH="develop"
# BASE_RAW_POCS_URL="https://raw.githubusercontent.com/${POCS_GITHUB_USER}/POCS/${POCS_BRANCH}"
# export AUTO_INSTALL_RAW_URL="${BASE_RAW_POCS_URL}/scripts/install/auto-install.sh"
# wget -q -O - "${AUTO_INSTALL_RAW_URL}" | bash

################################################################################
# Functions COPIED from install-helper-functions.sh
# Print the disk location of the first arg.
function safe_which() {
type -p "${1}" || /bin/true
}

# Print a separator bar of # characters.
function echo_bar() {
local terminal_width="${COLUMNS}"
if [ -z "${terminal_width}" ] && [ -n "${TERM}" ]
then
if [[ -n "$(safe_which tput)" ]]
then
terminal_width="$(tput cols)"
elif [[ -n "$(safe_which resize)" ]]
then
terminal_width="$(resize 2>/dev/null | grep COLUMNS= | cut -d= -f2)"
elif [[ -n "$(safe_which stty)" ]]
then
terminal_width="$(stty size 2>/dev/null | cut '-d ' -f2)"
fi
fi
printf "%${terminal_width:-80}s\n" | tr ' ' '#'
}

################################################################################

function do_sudo() {
if [ "$(id -u -n)" == "root" ] ; then
echo "Running ${*}"
"$@"
else
echo <<ENDOFMESSAGE
Running:
sudo ${*}
You may be prompted for your password.
ENDOFMESSAGE
(set -x ; sudo "$@")
fi
}

function clone_or_update() {
local -r REPO_DIR="${1}"
local -r REPO_URL="${2}"
local -r ORIGIN_NAME="${3}"
local -r BRANCH="${4}"

echo_bar

if [ ! -d "${REPO_DIR}/.git" ]
then
if [ -d "${REPO_DIR}" ]
then
echo 2> "
The directory (${REPO_DIR}) already exists, but doesn't appear
to be a valid git repository. Please remove it (or move it out
of the way) and re-run this script.
"
exit 1
fi
echo "
Cloning ${REPO_URL} into ${REPO_DIR}
"
(set -x ; git clone --origin "${ORIGIN_NAME}" "${REPO_URL}" "${REPO_DIR}")
cd "${REPO_DIR}"
git checkout "${BRANCH}"
jamessynge marked this conversation as resolved.
Show resolved Hide resolved
else
echo "
Pulling the latest software into the worktree at ${REPO_DIR}.
"
cd "${REPO_DIR}"
git fetch --all
git checkout "${BRANCH}"
git pull
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they have already set up the repo they might have origin set to their repo and this pull might not necessarily be doing what we want. Or they might have files in staging, in which case this will fail.

I think if the dir is there and a repo we just do nothing other than maybe print a message reminding them to update. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, clearly if there are already files staged then we need to avoid messing with the repo.
Of course, if they have a repo, they don't need this auto-install script, and can run install-dependencies.sh themselves.

I'll adjust the script to stop if there is a POCS or PAWS repo.

fi
}

function maybe_print_example() {
if [[ -n "${AUTO_INSTALL_RAW_URL}" ]]
then
>&2 cat <<ENDOFMESSAGE
For example:

wget -q -O - "${AUTO_INSTALL_RAW_URL}" | bash

ENDOFMESSAGE
fi
}

function ensure_ownership() {
local -r path="${1}"
local -r user="${2}"
local -r group="${3}"
echo_bar
echo "
Ensuring that ${path} is owned by user ${user}, and by group ${group}.
"
time do_sudo chown --recursive "${user}:${group}" "${path}"
echo
}

function ensure_directory_ownership() {
local -r path="${1}"
local -r user="${2}"
local -r group="${3}"

echo_bar
echo "
Ensuring that directory ${path} exists.
"
if [ ! -d "${path}" ]
then
do_sudo mkdir -p "${path}"
echo
fi

ensure_ownership "${path}" "${user}" "${group}"
}

function already_have_a_repo() {
>&2 cat <<ENDOFMESSAGE

The purpose of this script is to make sure git is installed, then to clone the
POCS and PAWS git repositories (which contain the PANOPTES software for a
PANOPTES telescope), install software that POCS and PAWS depend upon, and to
run tests. However, there is already a $1 repository located at:

$2

You may be able to install those dependencies by running this command:

${POCS}/scripts/install/install-dependencies.sh

ENDOFMESSAGE
}



# End of functions.
################################################################################

# Exit immediately if a command fails:
set -e

# COPIED from default-env-vars.sh
[[ -z "${PANUSER}" ]] && export PANUSER="panoptes" # Default user
[[ -z "${PANDIR}" ]] && export PANDIR="/var/panoptes" # Main Dir
[[ -z "${PANLOG}" ]] && export PANLOG="${PANDIR}/logs" # Log files
[[ -z "${POCS}" ]] && export POCS="${PANDIR}/POCS" # Main Observatory Control
[[ -z "${PAWS}" ]] && export PAWS="${PANDIR}/PAWS" # Web Interface


# Do we need to create the user PANUSER?

if ! id -u "${PANUSER}" 2>/dev/null 1>/dev/null
then
>&2 cat <<ENDOFMESSAGE
The user ${PANUSER} doesn't exist yet. Please create it by running:

sudo adduser --shell /bin/bash --add_extra_groups ${PANUSER}

You will be prompted to enter a password for this new user. You may also be
prompted to provide contact info as if this were a user on a shared computer
(e.g. Full Name and Home Phone). Press Enter to leave these unset.

After the command completes, we need to ensure that the user is a
member of some key groups:

sudo usermod --append --groups adm,dialout,plugdev,sudo ${PANUSER}

Next, login as ${PANUSER} and re-execute the command you used to
run this script.
ENDOFMESSAGE

maybe_print_example
exit 1
fi

# Do we need to login as the user PANUSER?

if [[ "$(whoami)" != "${PANUSER}" ]]
then
echo >&2 "
This script should be executed by the user ${PANUSER}, not as $(whoami).
Please login as ${PANUSER} and re-execute the command you used to
run this script.
"

maybe_print_example
exit 1
fi

# I (James Synge) have noticed that if $HOME/.cache/ doesn't exist, then at some
# point during the install it gets created, but is owned by root:root, which
# then messes up later steps that attempt to use it. So, we make sure here that
# it exists with the correct ownership.

PANGROUP="$(id -gn "${PANUSER}")"
ensure_directory_ownership "${HOME}/.cache" "${PANUSER}" "${PANGROUP}"

# Do the same with PANDIR (usually /var/panoptes).
ensure_directory_ownership "${PANDIR}" "${PANUSER}" "${PANGROUP}"

################################################################################
# Is there already a POCS or PAWS repository? If so, the user should not need
# this script.
if [[ -d "${POCS}/.git" ]]
then
already_have_a_repo POCS "${POCS}"
exit 1
fi

if [[ -d "${PAWS}/.git" ]]
then
already_have_a_repo PAWS "${PAWS}"
exit 1
fi

# Install git if necessary.

if [ ! -x "$(safe_which git)" ]
then
echo_bar
echo "
git is not installed. Updating package cache, then installing git.
"
do_sudo apt-get update
echo
echo
do_sudo apt-get install -y git
echo
fi

# Clone the POCS repo from github.

clone_or_update "${POCS}" https://github.com/panoptes/POCS.git upstream develop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want the upstream here? Seems like it is not set anywhere above if I'm following correctly. You do set it right below this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is used as the origin name when cloning, so that https://github.com/panoptes/POCS.git will be the upstream repo, and https://github.com/${POCS_GITHUB_USER}/POCS.git will be the origin.


# If the user specified another repo via POCS_GITHUB_USER, use that as
# the origin, and checkout the branch POCS_BRANCH.

if [[ -n "${POCS_GITHUB_USER}" && "${POCS_GITHUB_USER}" != "panoptes" ]]
then
cd "${POCS}"
git remote add -f origin "https://github.com/${POCS_GITHUB_USER}/POCS.git" || true
git checkout "origin/${POCS_BRANCH:-develop}"
fi

# Clone PAWS too, but only from the upstream.
clone_or_update \
"${PAWS}" "https://github.com/panoptes/PAWS.git" upstream develop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about upstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that is intended.


################################################################################
echo
echo_bar
echo_bar
echo "
Executing ${POCS}/scripts/install/install-dependencies.sh, which will
install the tools needed to run POCS.
"

${POCS}/scripts/install/install-dependencies.sh

echo
echo
echo_bar
echo_bar
echo "

Testing the installation of POCS. This can easily take 5 minutes on an Intel NUC
or longer on a Raspberry Pi.

"

if [[ ! -e "${POCS}/conf_files/pocs_local.yaml" ]]
then
# TODO Consider allowing the *_local.yaml files to be located outside of the
# /var/panoptes tree so that it is feasible to wipe out /var/panoptes and
# reinstall without losing local customizations.
cp "${POCS}/conf_files/pocs.yaml" "${POCS}/conf_files/pocs_local.yaml"
echo "

You will need to modify the file ${POCS}/conf_files/pocs_local.yaml
to suit your needs (e.g. set the unit name, location, etc.).
"
fi

# shellcheck source=/var/panoptes/set-panoptes-env.sh
source $PANDIR/set-panoptes-env.sh
cd $POCS
python setup.py install
pytest --test-databases=all --solve

if cmp "${POCS}/conf_files/pocs.yaml" "${POCS}/conf_files/pocs_local.yaml"
then
echo "

Don't forget to modify the file ${POCS}/conf_files/pocs_local.yaml
to suit your needs (e.g. set the unit name, location, etc.).
"
fi

exit
11 changes: 7 additions & 4 deletions scripts/install/install-apt-packages.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -ex
#!/bin/bash -e

# Installs APT packages (e.g. debian/ubuntu packages installed for all users).
# Requires being logged in as root or the ability to successfully execute sudo.
Expand All @@ -17,7 +17,7 @@ fi

# Generate the basic apt-get install command, minus the list of packages.
# We store into a shell array, i.e. an array of strings.
declare -a apt_get_install=(apt-get install --no-install-recommends --yes)
declare -a apt_get_install=(apt-get --quiet install --no-install-recommends --yes)
# shellcheck disable=SC2119
apt_proxy_url="$(get_apt_proxy_url)"
if [ -n "${apt_proxy_url}" ] ; then
Expand All @@ -26,17 +26,19 @@ fi

# Install all of the packages specified in the files in the args.
function install_apt_packages() {
echo_bar
echo
echo_running_sudo "apt-get update"
echo
my_sudo apt-get update
my_sudo apt-get --quiet update

for APT_PKGS_FILE in "$@"
do
echo_bar
echo
# Remove all the comments from the package list and install the packages whose
# names are left.
APT_PKGS="$(cut '-d#' -f1 "${APT_PKGS_FILE}" | sort | uniq)"
echo
echo_running_sudo "apt-get install for the files in ${APT_PKGS_FILE}"
echo
# A note on syntax: ${array_variable} expands to just the first element
Expand All @@ -46,6 +48,7 @@ function install_apt_packages() {
# a single element (e.g. "a b") doesn't result in multiple 'words'.
# shellcheck disable=SC2086
my_sudo "${apt_get_install[@]}" ${APT_PKGS}
echo
done
}

Expand Down
Loading