From 92828b07f1d7dcc2369bb3fc60311b0af7a1350b Mon Sep 17 00:00:00 2001 From: Joe Balough Date: Mon, 22 Aug 2016 13:53:24 -0400 Subject: [PATCH] lego: Initial commit of v2 -- Using Docker Add a completely new implementation of lego that uses Docker for its backend. Re-write README to describe setup. Remove completion since it no longer works. --- Makefile | 3 +- README | 84 ++----- lego | 632 +++++------------------------------------------- lego_completion | 33 --- 4 files changed, 93 insertions(+), 659 deletions(-) delete mode 100755 lego_completion diff --git a/Makefile b/Makefile index 0770c39..6bbe6f8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ -# Install lego to /usr/bin and its completion into /etc/bash_completion.d +# Install lego to /usr/bin install: install -m 755 lego /usr/bin - install -m 755 lego_completion /etc/bash_completion.d/ diff --git a/README b/README index 7a94da7..326b8f5 100644 --- a/README +++ b/README @@ -1,65 +1,33 @@ -lego -- build system management with schroot -Joe Balough 2012 +# `lego` -- build system management with docker -lego is a utility for managing btrfs-snapshot chroots using the schroot tool. -To use lego, create a new Logical Volume, format it btrfs, and mount it under /builds. -Under /builds, create two subdirectories, /builds/bases and /builds/chroots. -Create a new btrfs subvolume under /builds/bases and set up that directory as a build chroot: - Use debbootstrap to install a base operating system - Copy some default .basrhrc, etc. files to etc/skel in the base-chroot - Copy the sources list from the main system into the base-chroot for more repositories - Chroot into the base-chroot and install at least the following software: - build-essential subversion git vim diffstat texi2html texinfo cvs gawk chrpath wget screen \ - python python-apport python-apt python-apt-common python-argparse python-central python-crypto \ - python-gnupginterface python-httplib2 python-keyring python-launchpadlib python-lazr.restfulclient \ - python-lazr.uri python-minimal python-oauth python-pkg-resources python-ply python-problem-report \ - python-progressbar python-simplejson python-software-properties python-support python-wadllib \ - python-wsgi-intercept python-zope.interface sqlite3 autoconf libtool - Change the /bin/sh symlink so that it points to /bin/bash - Set up openembedded under /opt -Add the base-chroot to the schroot configuration file at /etc/schroot/schroot.conf - [beableboard] - type=btrfs-snapshot - description=BeagleBoard System Build - users=builder - root-users=builder - source-root-users=root - btrfs-source-subvolume=/builds/bases/bb-base - btrfs-snapshot-directory=/builds/chroots -Comment out home mount in /etc/schroot/default/fstab, otherwise the base server home directory is mounted there. -Add tmpfs mount point for /dev/shm (Required for python multithreading support): - /etc/schroot/default/fstab add to end of list: - tmpfs /dev/shm tmpfs defaults,noexec,nosuid 0 0 - and remove /builds/bases/bb-base/dev/shm - then # mkdir /builds/bases/bb-base/dev/shm -Create a script to ensure the user has a home directory inside the new chroot - /etc/schroot/setup.d/60home - set -e +## Joe Balough 2016 - . "$SETUP_DATA_DIR/common-data" - . "$SETUP_DATA_DIR/common-functions" - . "$SETUP_DATA_DIR/common-config" +lego is a utility for managing docker container chroots using the docker tool. +To use lego, it is best used in conjunction with a separate volume, formatted ext4, +and mounted to /var/lib/docker so that docker containers and their storage go there. +Follow the instructions for your distribution to change the docker launch options to +use the `overlay` or better `overlay2` storage backend. If you have to use `overlay` +make sure that your ext4 volume has a higher density of inodes to avoid running out. - # Plain chroots should not be able to use scripts, but check anyway to - # be safe. - if [ $CHROOT_TYPE = "plain" ]; then - fatal "plain chroots may not run setup scripts; use directory instead" - fi +With docker configured, start the service and make sure it all works ok. - # Set up home directory for user if it does not already exist - if [ ! -d "$CHROOT_MOUNT_LOCATION/$AUTH_HOME" ]; then - HOMEDIR=$(/usr/bin/dirname "$CHROOT_MOUNT_LOCATION/$AUTH_HOME") - if [ ! -d "$HOMEDIR" ]; then - mkdir -p "$HOMEDIR" - fi - cp -rf "$CHROOT_MOUNT_LOCATION/etc/skel" "$HOMEDIR/$AUTH_USER" - chown -R $AUTH_USER:$AUTH_USER "$HOMEDIR/$AUTH_USER" - chown -R $AUTH_USER:$AUTH_USER "$CHROOT_MOUNT_LOCATION/opt/oebuild/sources" - fi +Create a `Dockerfile` for your build environment. Base it off of the distribution you +are used to and install the packages your build tool needs inside. +The `Dockerfile` should add a common user that all users can use (like `builder`) +so that when you promote a chroot and other people use it, they can still build as +expected. -Base system should now be set up. -Run lego --help for usage information. +Build your `Dockerfile` and tag it to make your first base-chroot. + +From there, you can use lego just like it did with version 1. +The main difference is that chroot management is a bit better with docker. + +### Integration with Jenkins + +To use a Docker container to perform builds, make your build environment specify +SSHD to be the default RUN command. It should be possible to run one as a daemon +(with Docker) and ssh to the forwarded SSH port (as indicated in `docker ps`). +With this container, you can add it as an image in the Jenkins Docker plugin, then +limit builds to that container using the label. -Also included in this repository is a lego_completion script that will provide -bash completion for lego. Copy this script into /etc/bash_completion.d to enable it. diff --git a/lego b/lego index 9fb77d7..b53a09a 100755 --- a/lego +++ b/lego @@ -1,589 +1,89 @@ #!/bin/bash -# Build list of available chroots -SCRIPT_NAME=$(basename $0) -AVAIL_BASE=$(grep '^\[\(.*\)\]' /etc/schroot/schroot.conf 2> /dev/null | tr -d '[]') -AVAIL_PROMOTED_WORKING=$(head -qn 1 /etc/schroot/chroot.d/* 2> /dev/null | tr -d '[]') -AVAIL_TEMPLATE="$AVAIL_BASE $AVAIL_PROMOTED_WORKING" -AVAIL_WORKING=$(ls /builds/chroots 2> /dev/null) -ALL_AVAIL="$AVAIL_BASE $AVAIL_WORKING" - -# Sets the print color. -function SetColor -{ - case $1 in - red) - tput setaf 1;; - green) - tput setaf 2;; - yellow) - tput setaf 3;; - blue) - tput setaf 4;; - purple) - tput setaf 5;; - cyan) - tput setaf 6;; - white) - tput setaf 7;; - bold) - tput bold;; - uline) - tput sgr 0 1;; - *) - tput sgr0;; - esac -} - -# Usage: colorecho "color" "message that can be quite long" -# Prints message in color -function colorecho -{ - SetColor $1 - echo $2 - SetColor no -} - -# Prints the command usage -print_usage () -{ - echo "Usage $SCRIPT_NAME [command] ..." - echo "$SCRIPT_NAME is a tool to simplify the creation and running of build environment chroots on the machine." - echo "" - echo "There are three types of chroots referred to in the commands below:" - echo " base-chroot" - echo " Can only be used as a base for a persistent or non-persistent snapshot chroot." - echo " It is not possible to run these chroots are they are." - echo " base-chroot configuration is stored in /etc/schroot/schroot.conf" - echo "" - echo " work-chroot" - echo " A snapshot of a base-chroot that can then be chrooted to. These chroots are possible to be run as they are." - echo " work-chroots are of one of the following types:" - echo " persistent The chroot remains after you log out, able to be resumed using the run command." - echo " non-persistent The chroot is destroyed when you log out." - echo "" - echo " promoted work-chroot" - echo " A work-chroot that has been promoted remains a work-chroot that can be run as it is, however" - echo " it is also possible to use a promoted work-chroot as a template-chroot on which another work-chroot is based." - echo " Promoted work-chroot configurations are stored in /etc/schroot/chroot.d/(full-name).conf" - echo "" - echo "" - echo " [command] can be one of the following:" - echo "" - echo " list" - echo " Lists all defined base-chroots, promoted work-chroots, and non-promoted work-chroots available on the system." - echo "" - echo " new (template-chroot) (local-name)" - echo " Creates a new persistent work-chroot which is a snapshot of (template-chroot)." - echo " A template-chroot is either a base-chroot or a promoted work-chroot." - echo " The new work-chroot will use the following format to make its full name:" - echo " (base-chroot-name)=(username)-(local-name)." - echo " The automatic formatting will help prevent name collisions" - echo " and allows for the history of that chroot to be visible at a glance." - echo " Name collisions will result in an error." - echo "" - echo " run (full-name) [command ...]" - echo " Runs the base- or work-chroot with the specified full name." - echo " If (full-name) is a base-chroot, a non-persistent snapshot will be created and you will be immediately" - echo " chroot'd into that temporary environment *WHICH WILL BE DESTROYED WHEN YOU LOG OUT.*" - echo " If (full-name) is a promoted work-chroot, it will still chroot to that work-chroot that will remain persistent." - echo " If [command ...] arg is present, it will be run inside the specified chroot." +Usage() { + echo "Usage: $0 (command) [arg] ..." echo "" - echo " end (full-name)" - echo " Ends the work-chroot with specified full name." - echo " * THIS WILL DESTROY THE SPECIFIED CHROOT ENVIRONMENT! *" - echo " If the work-chroot is promoted, it will be automatically demoted before it is ended." - echo "" - echo " promote (full-name)" - echo " Promotes a work-chroot with specified (full-name) to a promoted work-chroot," - echo " allowing it to be used as a template chroot with the new command." - echo " This command will create a configuration file in /etc/schroot/chroot.d and thus requires root privilages." - echo "" - echo " demote (full-name)" - echo " Demotes a promoted work-chroot to a normal work-chroot," - echo " making it no longer able to be used as a template-chroot with the new command." - echo " This command will delete a configuration file from /etc/schroot/chroot.d and thus requires root privilages." - echo "" - echo " permit (full-name) (username)" - echo " Adds (username) to the list of users allowed to create a chroot based on (full-name)." - echo " The first time this command is run, the user is simply added to the schroot users list for that chroot." - echo " The second time this command is run, the user is added to the root-users list, allowing that user to use sudo inside the chroot." - echo "" - echo " prohibit (full-name) (username)" - echo " Removes (username) from both the users and root-users list for the (full-name) chroot." - echo "" -} - -# Determines if $1 is in the array $2 -in_array() -{ - needle=$1 - haystack=$2 - for i in $haystack; do - if [ "$i" == "$needle" ]; then - return 0 - fi - done - return 1 -} - -# Lists the available chroots in the system -list_chroots () -{ - if [ "$1" == "" ]; then - if [ "$AVAIL_BASE" == "" ]; then - colorecho red "No base-chroots defined." - colorecho red "Set up base-chroot(s) in /etc/schroot/schroot.conf" - return 1 - else - colorecho cyan "Defined base-chroots:" - for i in $AVAIL_BASE; do - echo -e "\t$i" - done - fi - echo "" - colorecho cyan "Defined work-chroots:" - if [ "$AVAIL_WORKING" == "" ]; then - echo -e "\tNo work-chroots have been created." - echo -e "\tCreate work-chroots using the new command." - else - for i in $AVAIL_WORKING; do - echo -e "\t$i" - done - fi - echo "" - colorecho cyan "Promoted work-chroots:" - if [ "$AVAIL_PROMOTED_WORKING" == "" ]; then - echo -e "\tNo promoted work-chroots have been created." - echo -e "\tPromote work-chroots using the promote command." - else - for i in $AVAIL_PROMOTED_WORKING; do - echo -e "\t$i" - done - fi - if [ "$AVAIL_TEMPLATE" != "" ]; then - echo "" - colorecho cyan "Defined template chroots (base-chroots and promote work-chroots):" - for i in $AVAIL_TEMPLATE; do - echo -e "\t$i" - done - fi - else - case "$1" in - template) - echo $AVAIL_TEMPLATE - ;; - working) - echo $AVAIL_WORKING - ;; - all) - echo $AVAIL_TEMPLATE $AVAIL_WORKING - ;; - esac - fi -} - - -# Creates a new chroot based on $2 named $3 -new_chroot() -{ - template_chroot=$1 - local_name=$2 - - if [ "$template_chroot" == "" ] || [ "$local_name" == "" ]; then - print_usage - echo "" - colorecho red "Template chroot name or local name missing." - return 1 - fi - - if ! in_array $template_chroot "$AVAIL_TEMPLATE"; then - colorecho red "Template chroot '$template_chroot' not found." - colorecho red "Not doing anything..." - return 1 - fi - - # Input checks out, create the chroot - output=$(/usr/bin/schroot -c $template_chroot -b -n "$template_chroot=$USER-$local_name" 2>&1) - exitval=$? - - # Print a status message - if [ $exitval -ne 0 ]; then - colorecho red "Failed to create work-chroot. schroot output:" - echo "$output" - colorecho red "Make sure a work-chroot with that name does not already exist." - return $exitval - else - colorecho green "Successfully created work-chroot '$output'" - return 0 - fi -} - -run_chroot() -{ - chroot_name=$1 - shift - - # Check input - if [ "$chroot_name" == "" ]; then - print_usage - echo "" - colorecho red "work-chroot name missing." - return 1 - fi - - if ! in_array $chroot_name "$ALL_AVAIL"; then - colorecho red "chroot '$chroot_name' not found." - colorecho red "Not doing anything..." - return 1 - fi - - # Clear the screen - clear - - # Handle starting a new temporary work-chroot from a base-chroot. Add a long warning before the chroot to tell the user about the danger. - if in_array $chroot_name "$AVAIL_BASE"; then - colorecho red "WARNING: '$chroot_name' is a base-chroot. A temporary snapshot will be created and chroot'd to." - colorecho red " THIS CHROOT ENVIRONMENT WILL BE DESTROYED AND LOST FOREVER WHEN YOU LOG OUT OF THE CHROOT." - colorecho red " Use the new command to create a persistent work-chroot." - schroot -c $chroot_name --directory=${HOME} -- $@ - exitval=$? - colorecho yellow "NOTE: Temporary work-chroot environment permanently deleted." - return $exitval - fi - # Must be good to switch to the persistent work-chroot - schroot -c $chroot_name -r --directory=${HOME} -- $@ -} - - -end_chroot() -{ - output=$1 - # Check input - if [ "$output" == "" ]; then - print_usage - echo "" - colorecho red "work-chroot name missing." - return 1 - fi - - if ! in_array $output "$AVAIL_WORKING"; then - colorecho red "work-chroot '$output' not found." - colorecho red "Not doing anything..." - return 1 - fi - - # Confirm with user that they want to end the chroot. Print a message telling them what exactly this means. - colorecho yellow "WARNING: Ending a work-chroot will permanently delete its environment." - colorecho yellow " What work has been done in this chroot will be lost forever." - - # See if the work-chroot is promoted - promoted="N" - if in_array $output "$AVAIL_PROMOTED_WORKING"; then - colorecho yellow "WARNING: work-chroot '$output' is promoted." - colorecho yellow " Ending this work-chroot will also make it impossible to create new chroots based on this work-chroot." - promoted="Y" - fi - echo "" - while [ "${input^^}" != "YES" ] && [ "${input^^}" != "NO" ]; do - if [ "$input" != "" ]; then - colorecho cyan "Must type yes or no" - fi - echo -n "Are you sure you want to continue (yes/no)? " - read input - done - if [ "${input^^}" != "YES" ]; then - colorecho red "Aborting." - return 1 - fi - - # If it was promoted, demote it first - if [ "$promoted" == "Y" ]; then - demote_chroot $output - if [ $? -ne 0 ]; then - return 1 - fi - fi - - # Delete chroot - schroot -c $output -e - colorecho green "Permenantly deleted chroot environment for '$output' work-chroot." - return $? -} - -promote_chroot() -{ - output=$1 - # Check input - if [ "$output" == "" ]; then - print_usage - echo "" - colorecho red "work-chroot name missing." - return 1 - fi - - if ! in_array $output "$AVAIL_WORKING"; then - colorecho red "work-chroot '$output' not found." - colorecho red "Not doing anything..." - return 1 - fi - - if in_array $output "$AVAIL_PROMOTED_WORKING"; then - colorecho yellow "work-chroot '$output' already promoted." - colorecho yellow "Not doing anything..." - return 0 - fi - - # Need to craft the new config file here - (cat < /etc/schroot/chroot.d/$output.conf - - if [ $? -ne 0 ]; then - colorecho red "Failed to promote work-chroot '$output'." - colorecho red "Make sure command is run as root." - return 1 - else - colorecho green "Successfully promoted work-chroot '$output'" - return 0 - fi -} - -demote_chroot() -{ - output=$1 - # Check input - if [ "$output" == "" ]; then - print_usage - echo "" - colorecho red "work-chroot name missing." - colorecho red "Not doing anythin..." - return 1 - fi - - # Make sure that chroot is actually promoted - if ! in_array $output "$AVAIL_PROMOTED_WORKING"; then - colorecho red "work-chroot '$output' not promoted." - colorecho red "Not doing anything..." - return 1 - else - # try to remove its configuration file. - rm -f /etc/schroot/chroot.d/$output.conf - - # See if the remove failed. Probably a permission issue if it did. - if [ $? -ne 0 ]; then - colorecho red "Failed to demote work-chroot '$output'." - colorecho red "Make sure command is run as root." - return 1 - else - colorecho green "Successfully demoted work-chroot '$output'" - return 0 - fi - fi + echo "Commands:" + echo " list - List defined base and work chroots" + echo " new (base-chroot) (name) - Create a new persistent chroot and run it" + echo " The container name will be like ${USER}-name" + echo " end (name) - Remove the named work chroot" + echo " run (name | base-chroot) - If provided a work-chroot, start the container and connect to a bash shell inside." + echo " If provided a base-chroot, start the indicated base-chroot and automatically remove it when exited." + echo " promote (name) - Promote the specified chroot so that new work-chroots can be made based off of it." + echo " demote (name) - Demote the specified chroot so that new work-chroots can no longer be made based off of it." + exit 1 } -permit_chroot() -{ - full_name=$1 - user_name=$2 - # Check input - if [ "$full_name" == "" ] || [ "$user_name" == "" ]; then - print_usage - echo "" - colorecho red "base-chroot / promoted work-chroot name or username missing." - colorecho red "Not doing anything..." - return 1 - fi - - # Make sure chroot exists - if ! in_array "$full_name" "$AVAIL_PROMOTED_WORKING" && ! in_array "$full_name" "$AVAIL_BASE"; then - colorecho red "chroot '$full_name' not a promoted work-chroot or a base-chroot." - colorecho red "Not doing anything..." - return 1 - fi - - # Make sure user exists - if ! id "$user_name" > /dev/null 2>&1; then - colorecho red "user '$user_name' does not exist in system." - colorecho red "Not doing anything..." - return 1 - fi - - # Input appears to be ok. Go ahead and add them to the list - # base-chroot - if in_array "$full_name" "$AVAIL_BASE"; then - # Make sure the user isn't already root - if sed -n ":a;N;\$!ba;s|.*\[$full_name\].*\nroot-users=\([^\n]*\).*|\1|p" /etc/schroot/schroot.conf | grep -E "^([^,]*,)*$user_name,?.*$" > /dev/null; then - colorecho yellow "User '$user_name' already has root permissions in chroot '$full_name'." - colorecho yellow "Not doing anything..." - return 0 - fi - - # See if the user is already permitted. If so, add them to the root-users lists - add_to_list="users" - if sed -n ":a;N;\$!ba;s|.*\[$full_name\].*\nusers=\([^\n]*\).*|\1|p" /etc/schroot/schroot.conf | grep -E "^([^,]*,)*$user_name,?.*$" > /dev/null; then - add_to_list="root-users" - fi - - # Add to the list - sed -i ":a;N;\$!ba;s|\(\[$full_name\].*\n$add_to_list=[^\n]*\)|\1,$user_name|" /etc/schroot/schroot.conf - if [ $? -ne 0 ]; then - colorecho red "Failed to permit user '$user_name' to use base-chroot '$full_name'." - colorecho red "Make sure command is run as root." - return 1 - else - colorecho green "Successfully permitted user '$user_name' to use base-chroot '$full_name'." - return 0 - fi - # work-chroot - else - # Make sure they aren't already root there - if sed -n "s|^root-users=\(.*\)|\1|p" "/etc/schroot/chroot.d/$full_name.conf" | grep -E "^([^,]*,)*$user_name,?.*$" > /dev/null; then - colorecho yellow "User '$user_name' already has root permissions in chroot '$full_name'." - colorecho yellow "Not doing anything..." - return 0 - fi - - # See if the user is already permitted. If so, add them to the root-users lists - add_to_list="users" - if sed -n "s|^users=\(.*\)|\1|p" "/etc/schroot/chroot.d/$full_name.conf" | grep -E "^([^,]*,)*$user_name,?.*$" > /dev/null; then - add_to_list="root-users" - fi - - # Add to list - sed -i "s|\(^$add_to_list=.*\)|\1,$user_name|" "/etc/schroot/chroot.d/$full_name.conf" - if [ $? -ne 0 ]; then - colorecho red "Failed to permit user '$user_name' to use work-chroot '$full_name'." - colorecho red "Make sure command is run as root." - return 1 - else - colorecho green "Successfully permitted user '$user_name' to use work-chroot '$full_name'." - return 0 - fi - fi +WorkChroots() { + docker ps -a --format '{{.Names}}' | grep -vE '(registry|jenkins)' } - -prohibit_chroot() -{ - full_name=$1 - user_name=$2 - # Check input - if [ "$full_name" == "" ] || [ "$user_name" == "" ]; then - print_usage - echo "" - colorecho red "base-chroot / promoted work-chroot name or username missing." - colorecho red "Not doing anything..." - return 1 - fi - - # Make sure chroot exists - if ! in_array "$full_name" "$AVAIL_PROMOTED_WORKING" && ! in_array "$full_name" "$AVAIL_BASE"; then - colorecho red "chroot '$full_name' not a promoted work-chroot or a base-chroot." - colorecho red "Not doing anything..." - return 1 - fi - - # Make sure user exists - if ! id "$user_name" > /dev/null 2>&1; then - colorecho red "user '$user_name' does not exist in system." - colorecho red "Not doing anything..." - return 1 - fi - - # Input appears to be ok. Go ahead and remove them to the list - if in_array "$full_name" "$AVAIL_BASE"; then - - # Remove the username from the config file. - if ! sed -i ":a;N;\$!ba;s|\(\[$full_name\].*\nusers=[^\n]*\),$user_name\([^\n]*\nroot-users=[^\n]*\),$user_name\([^\n]*\)|\1\2\3|" /etc/schroot/schroot.conf; then - colorecho red "Failed to prohibit user '$user_name' from base-chroot '$full_name'." - colorecho red "Make sure command is run as root." - return 1 - else - colorecho green "Successfully prohibited user '$user_name' from promoted base-chroot '$full_name'." - return 0 - fi - else - # Remove the username from the config file. - if ! sed -i "s|,*$user_name\(,*\)|\1|g" "/etc/schroot/chroot.d/$full_name.conf"; then - colorecho red "Failed to prohibit user '$user_name' from promoted base-chroot '$full_name'." - colorecho red "Make sure command is run as root." - return 1 - else - colorecho green "Successfully prohibited user '$user_name' from promoted base-chroot '$full_name'." - return 0 - fi - fi +BaseChroots() { + # Print all images in the format {image name}:{version}, remove jenkins, registry, and ubuntu images, and remove :latest text + docker images --format '{{.Repository}}:{{.Tag}}' | grep -vE '(jenkins|registry|ubuntu)' | sed 's|:latest||' } - - -# Main command switch block -# Basic Usage: buildchroot [command] COMMAND=$1 - case $COMMAND in - ls) - list_chroots $2 - exit 0; - ;; list) - list_chroots $2 - exit 0; - ;; - add) - # Same as new - new_chroot $2 $3 - exit $? + echo "Definted base-chroots:" + BaseChroots + echo "" + echo "Defined work-chroots:" + WorkChroots + exit 0 ;; new) - new_chroot $2 $3 - exit $? - ;; - run) - shift - run_chroot $@ - exit $? + BASE=$2 + NAME=$3 + + if [ "${NAME}" = "" ] || [ "${BASE}" = "" ]; then Usage; fi + + echo "Creating container with the name ${USER}-${NAME} and running it." + echo "This container will still be around when you exit so please remember to end it." + echo "" + docker run -it --name "${USER}-${NAME}" --user jenkins "${BASE}" /bin/bash + exit 0 ;; end) - end_chroot $2 - exit $? - ;; - promote) - promote_chroot $2 - exit $? - ;; - demote) - demote_chroot $2 - exit $? - ;; - permit) - permit_chroot $2 $3 - exit $? - ;; - prohibit) - prohibit_chroot $2 $3 - exit $? - ;; + NAME=$2 - --help) - print_usage + if [ "${NAME}" = "" ]; then Usage; fi + + if docker rm "${NAME}"; then + echo "Removed container with the name ${NAME}" + fi exit 0 ;; - -h) - print_usage + run) + NAME=$2 + if [ "${NAME}" = "" ]; then Usage; fi + + if WorkChroots | grep -q "^${NAME}\$"; then + echo "Running persistent chroot ${NAME}" + docker start -i "${NAME}" + elif BaseChroots | grep -q "^${NAME}\$"; then + echo "Starting transient chroot of base ${NAME}." + echo "WARNING: This chroot will be automatically cleaned up when you exit." + docker run --rm -it --name "${USER}-TRANSIENT" --user jenkins "${NAME}" /bin/bash + fi exit 0 ;; - *) - print_usage - exit 1 + promote) + + NAME=$2 + if [ "${NAME}" = "" ]; then Usage; fi + + BASE="$(docker ps -a --format '{{.Image}}' --filter name="${NAME}")" + docker commit ${NAME} "${BASE}:${NAME}" + ;; + demote) + NAME=$2 + if [ "${NAME}" = "" ]; then Usage; fi + + BASE="$(docker ps -a --format '{{.Image}}' --filter name="${NAME}")" + docker rmi "${BASE}:${NAME}" ;; esac + diff --git a/lego_completion b/lego_completion deleted file mode 100755 index a48e624..0000000 --- a/lego_completion +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -# This script handles bash completion for the lego script -_lego_completion() -{ - local cur prev opts - COMPREPLY=() - cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - # Override the default WORDBREAKS variable to NOT include the = character, since that is probably in a chroot name. - COMP_WORDBREAKS="\"'><;|&(: " - - case $prev in - lego) - reply="list new run end promote demote permit prohibit" - ;; - new) - reply=$(lego list template | sed 's|=|\\=|g') - ;; - promote|demote) - reply=$(lego list working | sed 's|=|\\=|g') - ;; - end|permit|prohibit|run) - reply=$(lego list all | sed 's|=|\\=|g') - ;; - *) - return 0; - ;; - esac - - COMPREPLY=( $(compgen -W "${reply}" -- ${cur}) ) -} -complete -F _lego_completion lego \ No newline at end of file