Skip to content

Commit

Permalink
refactor(#39): update meatbox scripts to be fully posix compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
meatwallace committed May 19, 2019
1 parent fd3e17a commit 73ff3f3
Show file tree
Hide file tree
Showing 30 changed files with 167 additions and 128 deletions.
8 changes: 4 additions & 4 deletions .bash_profile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# if [[ $- == *i* ]]; then
# shellcheck source=.bashrc
. "$HOME/.bashrc"
# fi
if [[ $- == *i* ]]; then
# shellcheck source=.bashrc
. "$HOME/.bashrc"
fi
2 changes: 1 addition & 1 deletion .bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ asdf_dir="$MEATBOX_LIBS_DIR/asdf"

# load `asdf`, our version manager
if [ -d "$asdf_dir" ]; then
# shellcheck source=.meatbox/libs/asdf/asdf.sh
# shellcheck source=libs/asdf/asdf.sh
. "$asdf_dir/asdf.sh"
fi

Expand Down
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
name: "Build Dockerfile"
command: |
apk add --no-cache git >/dev/null 2>&1
cd scripts/linux/alpine
cd ./scripts/alpine
GIT_COMMIT_SHA1=$CIRCLE_SHA1 ./build.sh
no_output_timeout: 30m

Expand All @@ -46,7 +46,7 @@ jobs:
name: "Build Dockerfile"
command: |
apk add --no-cache git >/dev/null 2>&1
cd scripts/linux/arch
cd ./scripts/arch
GIT_COMMIT_SHA1=$CIRCLE_SHA1 ./build.sh
no_output_timeout: 30m

Expand Down
4 changes: 0 additions & 4 deletions .zprofile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ if (( $#commands[(i)lesspipe(|.sh)] )); then
export LESSOPEN="| /usr/bin/env $commands[(i)lesspipe(|.sh)] %s 2>&-"
fi

# }}}

#: tmp directory {{{

# if we don't have a TMPDIR variable set or it's empty, export it
if [ -z "${TMPDIR}" ]; then
export TMPDIR="/tmp/zsh-${UID}"
Expand Down
14 changes: 8 additions & 6 deletions bin/git-clone-or-update
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

git_clone_or_update() {
local repo_url="$1"
local repo_dir="$2"
repo_url="$1"
repo_dir="$2"

# use the repository's name as the directory if we didn't explicitly pass one
if is empty "$repo_dir"; then
if [ -z "$repo_dir" ]; then
repo_dir="${repo_dir%.git}"
fi

if is not a directory "$repo_dir"; then
if [ ! -d "$repo_dir" ]; then
mkdir -p "$repo_dir"

git clone "$repo_url" "$repo_dir" >/dev/null
else
(
Expand Down
68 changes: 29 additions & 39 deletions bin/meatbox
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,53 @@ log_message() {
}

meatbox_run_script() {
directory="$1"
script="$2"
ctx="$1"
cmd="$2"

(cd "$MEATBOX_SCRIPTS_DIR/$directory" && "./$script")
}

meatbox_run_universal_script() {
script="$1"

meatbox_run_script "universal" "$script"
}

meatbox_run_os_script() {
script="$1"
os="$(uname -s | lowercase)"

if [ -f "$MEATBOX_SCRIPTS_DIR/$os/$script" ]; then
meatbox_run_script "$os" "$script"
if [ -f "$MEATBOX_SCRIPTS_DIR/$ctx/$cmd.sh" ]; then
(cd "$MEATBOX_SCRIPTS_DIR/$ctx" && "./$cmd.sh")
fi
}

meatbox_help() {
usage
}
meatbox_run() {
meatbox_run_script "$kernel" "$cmd"

if [ "$kernel" = "linux" ]; then
distro=""

meatbox_bootstrap() {
start-sudo-loop "$MEATBOX_PASSWORD"
# cat /etc/*release

meatbox_run_os_script "bootstrap.sh"
meatbox_run_universal_script "bootstrap.sh"
}
# ID=alpine
# NAME="Alpine Linux"

if [ "$1" = "bootstrap" ]; then
meatbox_bootstrap
# ID=antergos
# ID_LIKE=archlinux
# NAME="Antergos Linux"
# PRETTY_NAME="Antergos Linux"

exit 0
fi
# DISTRIB_ID=Arch
# NAME="ArchBang Linux"
# ID=arch
# PRETTY_NAME="ArchBang Linux"

meatbox_setup() {
start-sudo-loop "$MEATBOX_PASSWORD"
meatbox_run_script "$distro" "$cmd"
fi

meatbox_run_os_script "setup.sh"
meatbox_run_universal_script "setup.sh"
meatbox_run_script "agnostic" "$cmd"
}

meatbox_update() {
start-sudo-loop "$MEATBOX_PASSWORD"

meatbox_run_os_script "update.sh"
meatbox_run_universal_script "update.sh"
meatbox_help() {
usage
}

meatbox() {
cmd="$1"

case "$cmd" in
bootstrap|setup|update) "meatbox_$cmd" ;;
bootstrap|setup|update)
start-sudo-loop "$MEATBOX_PASSWORD"
meatbox_run "$cmd"
;;
*) meatbox_help ;;
esac
}
Expand Down
27 changes: 27 additions & 0 deletions bin/meatman
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

set -o errexit -o nounset

# TODO: requires much smarter implementation w/ handling of various managers,
# operations, and flags
meatman() {
cmd="$1"
packages="$2"

if [ ! "$cmd" = "add" ]; then
echo "command unsupported"
exit 1
fi

# TODO: handle flags
if [ -x "$(command -v pacman)" ]; then
sudo pacman -S --noconfirm --needed $packages
elif [ -x "$(command -v apk)" ]; then
sudo apk add --no-cache $packages
else
echo "unsupported distro"
exit 1
fi
}

meatman "$@"
2 changes: 1 addition & 1 deletion bin/pacfast
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ download_packages() {
--dir="$cache_dir"

rm "$dl_list_file"
}
}

# if we have unfinished jobs from a previous run, complete them
if [ -f "$dl_list_file" ]; then
Expand Down
6 changes: 3 additions & 3 deletions bin/start-sudo-loop
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
#!/bin/sh

set -eo pipefail
set -e

start_sudo_loop() {
local password="$1"
password="$1"

if [ -n "$password" ]; then
echo "$password" | sudo -Sv
Expand Down
13 changes: 8 additions & 5 deletions index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ export MEATBOX_SCRIPTS_DIR="$MEATBOX_DIR/scripts"
cd "$HOME"

# ensure the user has a /bin directory, and add it to the path
mkdir "$MEATBOX_BIN_DIR"
if [ ! -d "$MEATBOX_BIN_DIR" ]; then
mkdir "$MEATBOX_BIN_DIR"
fi

export PATH="$MEATBOX_BIN_DIR:$PATH"

# pull down `yadm` and symlink it into our user's /bin
YADM_DIR="$MEATBOX_LIBS_DIR/yadm"
yadm_dir="$MEATBOX_LIBS_DIR/yadm"

if [ ! -d "$YADM_DIR" ]; then
git clone https://github.com/TheLocehiliosan/yadm.git "$YADM_DIR" >/dev/null
if [ ! -d "$yadm_dir" ]; then
git clone https://github.com/thelocehiliosan/yadm.git "$yadm_dir" >/dev/null

ln -fs "$YADM_DIR/yadm" "$MEATBOX_BIN_DIR/yadm"
ln -fs "$yadm_dir/yadm" "$MEATBOX_BIN_DIR/yadm"
fi

# clone our system config and update origin to use SSL in the future
yadm clone -f https://github.com/meatwallace/dotfiles >/dev/null
yadm remote set-url origin "git@github.com:meatwallace/dotfiles.git"

# check out a specific commit if it's been specified e.g. CI pull request
if [ -n "$MEATBOX_CHECKOUT_SHA1" ]; then
yadm checkout "$MEATBOX_CHECKOUT_SHA1"
fi
3 changes: 2 additions & 1 deletion scripts/agnostic/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/sh

set -eu
set -o errexit -o errexit

scripts="
install_yarn.sh
install_or_update_asdf.sh
"

Expand Down
10 changes: 7 additions & 3 deletions scripts/agnostic/install_or_update_asdf.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#!/bin/sh
#!/usr/bin/env bash

set -eu
set -euo pipefail

asdf_dir="$MEATBOX_LIBS_DIR/asdf"

if [ ! -x "$(command -v asdf)" ]; then
# TODO(#27): create an `unzip` install script as `lua` requires it
sudo pacman -Sy --noconfirm unzip >/dev/null
if [ -x "$(command -v pacman)" ]; then
sudo pacman -Sy --noconfirm unzip >/dev/null
else
sudo apk add --no-cache unzip
fi

# clone in our version manager
git clone https://github.com/asdf-vm/asdf.git "$asdf_dir" >/dev/null
Expand Down
4 changes: 2 additions & 2 deletions scripts/agnostic/install_or_update_python_packages.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

pip install --user --upgrade \
pip \
Expand Down
4 changes: 2 additions & 2 deletions scripts/agnostic/install_or_update_zplugin.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

sh -c "$(curl -fsSL https://raw.githubusercontent.com/meatwallace/zplugin/master/doc/install.sh)"
10 changes: 10 additions & 0 deletions scripts/agnostic/install_yarn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -o errexit -o nounset


install_yarn() {
meatman add yarn
}

install_yarn "$@"
4 changes: 2 additions & 2 deletions scripts/agnostic/load_zsh.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#!/bin/sh

set -euo pipefail
set -eu

# fire up zsh to trigger zplugin initialization
zsh
2 changes: 1 addition & 1 deletion scripts/agnostic/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -eu

declare scripts="
scripts="
install_or_update_asdf.sh
install_or_update_node_modules.sh
install_or_update_python_packages.sh
Expand Down
29 changes: 17 additions & 12 deletions scripts/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ ARG USER_SUDOERS_FILE="10-user"
# specific to this system's setup
ARG MEATBOX_USER="meatwallace"
ARG MEATBOX_PASSWORD
ARG MEATBOX_SETUP_SUDOERS_FILE="20-meatbox-setup"
ARG MEATBOX_SETUP_SUDOERS_FILE="20-setup"

RUN \
# add our user account, ensuring it's in the `wheel` group for `sudo`
adduser -D -s /bin/sh "${MEATBOX_USER}" users && \
# set the account's password
# install the bare minimum dependencies for this setup & our dots to work
apk add --no-cache bash curl git sudo && \
# add our user account & set the password
adduser -D -s /bin/bash "${MEATBOX_USER}" users && \
echo "${MEATBOX_USER}:${MEATBOX_PASSWORD}" | chpasswd && \
# add a sudo config file that allows our user to use `sudo`
echo "${MEATBOX_USER} ALL=(ALL) ALL" >> "/etc/sudoers.d/${USER_SUDOERS_FILE}" && \
Expand All @@ -22,10 +23,8 @@ RUN \
# add a temporary sudo config file allowing our user to use pacman & make without
# a password so our setup script doesn't require a custom `askpass` just for
# docker
echo "${MEATBOX_USER} ALL=NOPASSWD: /bin/sh, /bin/ln, /sbin/apk, /usr/sbin/addgroup, /sbin/rc-update, /sbin/rc-service" >> "/etc/sudoers.d/${MEATBOX_SETUP_SUDOERS_FILE}" && \
chmod 0440 "/etc/sudoers.d/${MEATBOX_SETUP_SUDOERS_FILE}" && \
apk update && \
apk add --no-cache curl
echo "${MEATBOX_USER} ALL=NOPASSWD: ALL" >> "/etc/sudoers.d/${MEATBOX_SETUP_SUDOERS_FILE}" && \
chmod 0440 "/etc/sudoers.d/${MEATBOX_SETUP_SUDOERS_FILE}"

# swap into our user account
USER "${MEATBOX_USER}"
Expand All @@ -35,7 +34,13 @@ WORKDIR "/home/${MEATBOX_USER}"
ARG MEATBOX_CHECKOUT_SHA1

RUN \
curl https://meatbox.one | sh && \
meatbox bootstrap && \
meatbox setup && \
rm -rf tmp/* /var/cache/apk/*
# run our system setup script from our staging alias
curl "https://meatbox.meatwallace.now.sh" | MEATBOX_CHECKOUT_SHA1="${MEATBOX_CHECKOUT_SHA1}" bash && \
. "/home/${MEATBOX_USER}/.bashrc" && \
meatbox bootstrap && \
. "/home/${MEATBOX_USER}/.bashrc" && \
meatbox setup && \
. "/home/${MEATBOX_USER}/.bashrc" && \
rm -rf tmp/* /var/cache/apk/*

CMD ["/bin/zsh"]
6 changes: 3 additions & 3 deletions scripts/alpine/add_edge_repositories.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

set -eu

sudo echo "@edge http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories
sudo echo "@edgecommunity http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
sudo echo "@testing http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
echo "@edge http://dl-cdn.alpinelinux.org/alpine/edge/main" | sudo tee -a /etc/apk/repositories
echo "@edgecommunity http://dl-cdn.alpinelinux.org/alpine/edge/community" | sudo tee -a /etc/apk/repositories
echo "@testing http://dl-cdn.alpinelinux.org/alpine/edge/testing" | sudo tee -a /etc/apk/repositories
2 changes: 0 additions & 2 deletions scripts/alpine/enable_services.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -eu

username="$(id -u -n)"

# lightdm
# libvirtd
# virtlogd
Expand Down
Loading

0 comments on commit 73ff3f3

Please sign in to comment.