Preserving .git.

Preserving .git.

#!/bin/bash

# --------------------------------------------------------------------------
#
# microbe: A pathogen-based Plugin Manager for GitHub-hosted Repositories.
#   Copyright (c) 2013 Yannick Scherer (yannick.scherer@gmail.com)
#
# --------------------------------------------------------------------------
# Normalize Home
which readlink >& /dev/null
if [[ "$?" == "0" ]]
then
    h="`readlink -f "$HOME"`"
    HOME="$h"
fi

# Read Configuration
if [ -s "$HOME/.microbe.conf" ]; then
    eval "`cat "$HOME/.microbe.conf"`"
fi

# Microbe Data
VERSION="0.2.0-SNAPSHOT"
VIMDIR="$HOME/.vim"
AUTOLOAD="$VIMDIR/autoload"
BUNDLE="$VIMDIR/bundle"
PATHOGEN_VIM="$AUTOLOAD/pathogen.vim"
MICROBE="$REPO"
if [ -z "$MICROBE" ]; then MICROBE="$HOME/.microbe"; fi
if [ -z "$COLORS" ]; then COLORS="yes"; fi

# Remote Data
PATHOGEN_REMOTE="https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim"
GITHUB_HTTPS="https://github.com"
DEFAULT_USER="vim-scripts"

# Script Data
SELF="`dirname "$0"`"
COMMAND="$1"
VERBOSE="$VERBOSE"
DEBUG="$DEBUG"
if [[ "$DEBUG" == "yes" ]]; then VERBOSE="yes"; fi

# --------------------------------------------------------------------------
# Colorize Output
GREEN="`tput setaf 2`"
YELLOW="`tput setaf 3`"
RED="`tput setaf 1`"
RESET="`tput sgr0`"

function echo_color() {
    if [[ "$COLORS" == "yes" ]]; then
        c="$1"
        shift
        echo -n ${c}${@}${RESET};
    else
        shift
        echo -n "$@";
    fi
}
function green() { echo_color "${GREEN}" "${@}"; }
function red() { echo_color "${RED}" "${@}"; }
function yellow() { echo_color "${YELLOW}" "${@}"; }
function echo_green() { green $@; echo; }
function echo_red() { red $@; echo; }
function echo_yellow() { yellow $@; echo; }

# Output
function verbose() {
    if [[ "$VERBOSE" != "no" ]] || [ -z "$VERBOSE" ]; then
        echo "$@"
    fi
}

function debug() {
    if [[ "$DEBUG" == "yes" ]]; then
        echo "[debug]" "$@"
    fi
}

function success() {
    verbose "`green "$@"`"
}

function error() {
    echo_red $@ 1>&2;
    exit 1;
}

# --------------------------------------------------------------------------
# Utilities

function check_git_repository() {
    which curl >& /dev/null || error "Dependency missing: curl";
    local url="$1"
    debug "Checking: $url"
    curl -o /dev/null --head --silent --fail "$url"
    local st="$?"
    debug "curl returned status $st."
    if [[ "$st" != "0" ]]; then return 1; fi
    return 0;
}

function link_microbe_repository() {
    local user="$1"
    local repo="$2"
    local src="$MICROBE/$user/$repo";
    local dst="$BUNDLE/${user}_${repo}"

    if [ ! -e "$dst" -o -L "$dst" ]; then
        ln -sf "$src" "$dst"
    else
        error "Cannot create Link at: $dst";
    fi
}

function load_git_repository() {
    which git >& /dev/null || error "Dependency missing: git";
    local user="$1"
    local repo="$2"
    local url="$3"
    local src="$MICROBE/$user/$repo";

    set -e
    debug "microbe directory: $src"
    debug "plugin directory:  $dst"
    if [ -d "$src" ]; then
        verbose -n "  - Using package from Cache ...";
        set +e
        return 0;
    fi

    verbose "  - Cloning from `yellow "$url"` ..."
    if [[ "$VERBOSE" != "no" ]]; then
        verbose ""
        git clone --depth=1 "$url" "$src"
        verbose ""
    else
        set +e
        local tmp="`mktemp`"
        git clone --depth=1 "$url" "$src" >& "$tmp";
        if [[ "$?" != "0" ]]; then
            cat "$tmp" 1>&2;
            rm "$tmp";
            exit 1;
        fi
        rm  "$tmp";
        set -e
    fi

    verbose -n "  - Adding to Pathogen Bundles ... "
    set +e
    return 0
}

function load_zip_repository() {
    local user="$1"
    local repo="$2"
    local url="$3"
    local tmp="`mktemp -d`"
    local dst="$MICROBE/$user/$repo"

    mkdir -p "$MICROBE/$user"
    verbose -n "  - Getting ZIP from `yellow "$url"` ... "
    curl -Sso "$tmp/archive.zip" -L --fail "$url"
    local st="$?"
    if [[ "$st" != "0" ]]; then error "Failed ($st)."; fi
    success "OK."

    verbose -n "  - Extracting Archive to `yellow "$dst"` ... "
    unzip "$tmp/archive" -d "$tmp" 1> /dev/null
    if [[ "$?" != "0" ]]; then error "Failed."; fi
    mv "$tmp/$repo-master" "$dst"
    rm -rf "$tmp"
    success "OK."
    verbose -n "  - Adding to Pathogen Bundles ... "
}

# --------------------------------------------------------------------------
# Actions

function action_help() {
    echo "Usage: $0 <Command> [<Parameters> ...]"
}

function action_version() {
    verbose "microbe `yellow "$VERSION"` (bash $BASH_VERSION)"
}

function action_init() {

    which curl >& /dev/null || error "Dependency missing: curl";
    which git >& /dev/null || error "Dependency missing: git";

    debug "curl:                 `which curl`"
    debug "git:                  `which git`"
    debug "microbe repository:   $MICROBE"
    debug "home path:            $HOME"
    debug "vim autoload path:    $AUTOLOAD"
    debug "pathogen bundle path: $AUTOLOAD"
    debug "pathogen path:        $PATHOGEN_VIM"

    set -e
    for dir in "$AUTOLOAD" "$BUNDLE" "$MICROBE"; do
        if [ ! -d "$dir" ]; then
            verbose -n "* Initializing '`basename "$dir"`' ... "
            mkdir -p "$dir"
            success "OK."
        fi
    done
    if [ ! -s "$PATHOGEN_VIM" ]; then
        verbose -n "* Loading Pathogen ... "
        curl -Sso "$PATHOGEN_VIM" "$PATHOGEN_REMOTE"
        success "OK."
    fi

    if [ ! -s "$HOME/.vimrc" ]; then
        verbose -n "* Creating '$HOME/.vimrc' ... "
        echo "set nocompatible" > "$HOME/.vimrc"
        echo "call pathogen#infect()" >> "$HOME/.vimrc"
        success "OK."
    else
        set +e
        grep -qx "call pathogen#infect()" "$HOME/.vimrc" >& /dev/null
        if [[ "$?" != "0" ]]; then
            set -e
            verbose -n "* Adding Pathogen to '$HOME/.vimrc' ... "
            mv "$HOME/.vimrc" "$HOME/.vimrc.bak"
            echo "set nocompatible" > "$HOME/.vimrc"
            echo "call pathogen#infect()" >> "$HOME/.vimrc"
            cat "$HOME/.vimrc.bak" >> "$HOME/.vimrc"
            success "OK."
        fi
    fi

    set +e
}

function action_install_single() {
    local spec="$1"

    if [ -z "$spec" ]; then
        echo_red "Usage: $0 install <spec>" 1>&2;
        echo "<spec> is one of:" 1>&2
        echo "- <GitHub User>/<Repository>" 1>&2
        echo "- <vim-scripts Repository>" 1>&2
        echo "- git://..." 1>&2
        exit 1;
    fi

    # Data
    local user=""
    local repo=""
    local url=""
    local resolve="yes"
    local zip="yes"

    # Git Repository?
    if [ ${spec:0:6} == "git://" ] || [[ "$spec" == *@* ]] || [ ${spec:0:8} == "https://" ]; then
        local user="external"
        local repo="`basename "$spec" ".git"`"
        local url="$spec"
        local resolve="no"
        local zip="no"

    # GitHub Repository with given User?
    else if [[ "$spec" == */* ]]; then
        IFS="/" read -r user repo <<< "$spec"
        local url="$GITHUB_HTTPS/$user/$repo"

    # GitHub Repository of vim-scripts?
    else
        local user="$DEFAULT_USER"
        local repo="$spec"
        local url="$GITHUB_HTTPS/$user/$repo"
    fi; fi

    # Initialization
    action_init

    # Repository in Cache?
    local path="$MICROBE/$user/$repo"
    if [ -d "$path" ]; then
        verbose "* Using `yellow "$user/$repo"` from Cache.";
        link_microbe_repository "$user" "$repo"
        return 0;
    else if [ -d "$path.vim" ]; then
        verbose "* Using `yellow "$user/$repo.vim"` from Cache.";
        link_microbe_repository "$user" "$repo.vim"
        return 0;
    fi; fi

    echo "* Installing `yellow "$user/$repo"` ..."

    # Resolve?
    if [[ "$resolve" == "yes" ]]; then
        verbose -n "  - Resolving Package `yellow "$repo"` ... "
        checked_url=""
        for u in "" ".vim"; do
            if check_git_repository "$url$u"; then
                checked_url="$url$u";
                repo="$repo$u";
                break;
            fi
        done
        if [ -z "$checked_url" ]; then error "Could not find Repository."; fi
        success "OK."
    else
        checked_url="$url"
    fi

    # Load Repository
    if [[ "$zip" == "yes" ]]; then
        zipUrl="$checked_url/archive/master.zip"
        load_zip_repository "$user" "$repo" "$zipUrl"
    else
        load_git_repository "$user" "$repo" "$checked_url"
    fi

    #
    link_microbe_repository "$user" "$repo"

    # Delete everything that is not needed
    set -e
    for x in `find "$MICROBE/$user/$repo" -mindepth 1 -maxdepth 1 2> /dev/null`;
    do
        local n="`basename "$x"`"
        case "$n" in
            syntax|indent|autoload|colors|ftplugin|*.vim|doc|.git)
                ;;
            *)
                rm -r "$x"
                ;;
        esac
    done

    # Write Metadata
    echo "$spec" > "$MICROBE/$user/$repo/.microbe_spec"
    success "OK."
    set +e
}

function action_install() {
    while [ $# -gt 0 ]; do
        action_install_single "$1"
        shift
    done
}

function action_remove_single() {
    local spec="$1"
    IFS="/" read -r user pckg <<< "$spec"

    # Check
    if [ -z "$user" -a -z "$pckg" ]; then
        error "Usage: $0 install [<GitHub User>/]<Package>"
    fi

    # Fallback to Default User?
    if [ -z "$pckg" ]; then
        pckg="$user"
        user="$DEFAULT_USER"
    fi

    # Remove
    set -e
    for r in "$pckg" "$pckg.vim";
    do
        path="$MICROBE/$user/$r"
        if [ -d "$path" ]; then
            verbose -n "* Removing `yellow "$user/$r"` ... "
            if [ -L "$BUNDLE/${user}_${r}" ]; then rm "$BUNDLE/${user}_${r}"; fi
            success "OK."

        fi
    done
    set +e
}

function action_remove() {
    while [ $# -gt 0 ]; do
        action_remove_single "$1"
        shift
    done
}

function action_purge_single() {
    local spec="$1"
    IFS="/" read -r user pckg <<< "$spec"

    # Check
    if [ -z "$user" -a -z "$pckg" ]; then
        error "Usage: $0 install [<GitHub User>] <Package>"
    fi

    # Fallback to Default User?
    if [ -z "$pckg" ]; then
        pckg="$user"
        user="$DEFAULT_USER"
    fi

    # Remove
    set -e
    for r in "$pckg" "$pckg.vim";
    do
        path="$MICROBE/$user/$r"
        if [ -d "$path" ]; then
            verbose -n "* Purging `yellow "$user/$r"` ... "
            rm -rf "$path"
            if [ -L "$BUNDLE/${user}_${r}" ]; then rm "$BUNDLE/${user}_${r}"; fi
            success "OK."

        fi
    done
    set +e
}

function action_purge() {
    while [ $# -gt 0 ]; do
        action_purge_single "$1"
        shift
    done
}

function action_update_single() {
    local specFile="$1"
    local repoDir="`dirname "$specFile"`"
    local repo="`basename "$repoDir"`"
    local userDir="`dirname "$repoDir"`"
    local user="`basename "$userDir"`"

    verbose -n "* Updating `yellow "$user/$repo"` ... "
    if [ -d "$repoDir/.git" ]; then
        cd "$repoDir"
        git pull -q
    else
        local spec="`cat "$specFile"`"
        local installed="yes"
        if [ ! -L "$BUNDLE/${user}_${repo}" ]; then installed="no"; fi
        action_purge "$user/$repo" >& /dev/null
        action_install "$spec" 1> /dev/null
        if [ "$installed" == "no" ]; then rm "$BUNDLE/${user}_${repo}"; fi
    fi
    success "OK."
}

function action_update() {

    # Update all?
    if [ "$#" == "0" ]; then
        set -e
        for f in `find "$MICROBE" -mindepth 3 -maxdepth 3 -type f -name ".microbe_spec" 2> /dev/null | sort`; do
            action_update_single "$f"
        done
        set +e
    else
        while [ $# -gt 0 ]; do
            local spec="$1"
            IFS="/" read -r user pckg <<< "$spec"

            # Fallback to Default User?
            if [ -z "$pckg" ]; then
                pckg="$user"
                user="$DEFAULT_USER"
            fi

            # Update
            set -e
            local path="$MICROBE/$user/$pckg"
            if [ ! -e "$path/.microbe_spec" ]; then path="$path.vim"; fi
            if [ ! -e "$path/.microbe_spec" ]; then shift; continue; fi
            action_update_single "$path/.microbe_spec"
            set +e
            shift
        done
    fi
}

function action_update_pathogen() {
    if [ ! -s "$PATHOGEN_VIM" ]; then
        action_init;
    else
        verbose -n "* Updating Pathogen ... "
        mv "$PATHOGEN_VIM" "$PATHOGEN_VIM.bak"
        curl -Sso "$PATHOGEN_VIM" "$PATHOGEN_REMOTE"
        if [[ "$?" != "0" ]]; then
            error "Could not download Pathogen."
            mv "$PATHOGEN_VIM.bak" "$PATHOGEN_VIM"
        else
            rm "$PATHOGEN_VIM.bak"
            success "OK."
        fi
    fi
}

function action_list() {
    set -e
    for dir in `find "$MICROBE" -mindepth 3 -maxdepth 3 -type f -name ".microbe_spec" 2> /dev/null | sort`; do
        local repoDir="`dirname "$dir"`"
        local repo="`basename "$repoDir"`"
        local userDir="`dirname "$repoDir"`"
        local user="`basename "$userDir"`"

        local s="`du -sk "$repoDir"`"
        local kb=""
        local r=""
        read kb r <<< "$s"

        local installed="(not installed)"
        if [ -L "$BUNDLE/${user}_${repo}" ]; then local installed="    `green "(installed)"`"; fi
        local line="$(printf "%50s     %s     %6s    %s" "$user/`yellow "$repo"`" "$installed" "${kb}KB" "$repoDir")"

        verbose "$line"
    done
    set +e
}

# --------------------------------------------------------------------------
# Handlers
if [ -z "$COMMAND" ] || [[ "$COMMAND" == "help" ]]; then
    action_help
    exit 0;
fi

shift
case "$COMMAND" in
    "version")
        action_version
        ;;
    "init")
        action_init
        ;;
    "install")
        action_install "$@"
        ;;
    "remove")
        action_remove "$@"
        ;;
    "purge")
        action_purge "$@"
        ;;
    "update")
        action_update "$@"
        ;;
    "update-pathogen")
        action_update_pathogen
        ;;
    "list")
        action_list
        ;;
    *)
        error "Unknown Action: $COMMAND";
        ;;
esac

#!/bin/bash

# --------------------------------------------------------------------------
#
# microbe: A pathogen-based Plugin Manager for GitHub-hosted Repositories.
#   Copyright (c) 2013 Yannick Scherer (yannick.scherer@gmail.com)
#
# --------------------------------------------------------------------------
# Normalize Home
which readlink >& /dev/null
if [[ "$?" == "0" ]]
then
    h="`readlink -f "$HOME"`"
    HOME="$h"
fi

# Read Configuration
if [ -s "$HOME/.microbe.conf" ]; then
    eval "`cat "$HOME/.microbe.conf"`"
fi

# Microbe Data
VERSION="0.2.0-SNAPSHOT"
VIMDIR="$HOME/.vim"
AUTOLOAD="$VIMDIR/autoload"
BUNDLE="$VIMDIR/bundle"
PATHOGEN_VIM="$AUTOLOAD/pathogen.vim"
MICROBE="$REPO"
if [ -z "$MICROBE" ]; then MICROBE="$HOME/.microbe"; fi
if [ -z "$COLORS" ]; then COLORS="yes"; fi

# Remote Data
PATHOGEN_REMOTE="https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim"
GITHUB_HTTPS="https://github.com"
DEFAULT_USER="vim-scripts"

# Script Data
SELF="`dirname "$0"`"
COMMAND="$1"
VERBOSE="$VERBOSE"
DEBUG="$DEBUG"
if [[ "$DEBUG" == "yes" ]]; then VERBOSE="yes"; fi

# --------------------------------------------------------------------------
# Colorize Output
GREEN="`tput setaf 2`"
YELLOW="`tput setaf 3`"
RED="`tput setaf 1`"
RESET="`tput sgr0`"

function echo_color() {
    if [[ "$COLORS" == "yes" ]]; then
        c="$1"
        shift
        echo -n ${c}${@}${RESET};
    else
        shift
        echo -n "$@";
    fi
}
function green() { echo_color "${GREEN}" "${@}"; }
function red() { echo_color "${RED}" "${@}"; }
function yellow() { echo_color "${YELLOW}" "${@}"; }
function echo_green() { green $@; echo; }
function echo_red() { red $@; echo; }
function echo_yellow() { yellow $@; echo; }

# Output
function verbose() {
    if [[ "$VERBOSE" != "no" ]] || [ -z "$VERBOSE" ]; then
        echo "$@"
    fi
}

function debug() {
    if [[ "$DEBUG" == "yes" ]]; then
        echo "[debug]" "$@"
    fi
}

function success() {
    verbose "`green "$@"`"
}

function error() {
    echo_red $@ 1>&2;
    exit 1;
}

# --------------------------------------------------------------------------
# Utilities

function check_git_repository() {
    which curl >& /dev/null || error "Dependency missing: curl";
    local url="$1"
    debug "Checking: $url"
    curl -o /dev/null --head --silent --fail "$url"
    local st="$?"
    debug "curl returned status $st."
    if [[ "$st" != "0" ]]; then return 1; fi
    return 0;
}

function link_microbe_repository() {
    local user="$1"
    local repo="$2"
    local src="$MICROBE/$user/$repo";
    local dst="$BUNDLE/${user}_${repo}"

    if [ ! -e "$dst" -o -L "$dst" ]; then
        ln -sf "$src" "$dst"
    else
        error "Cannot create Link at: $dst";
    fi
}

function load_git_repository() {
    which git >& /dev/null || error "Dependency missing: git";
    local user="$1"
    local repo="$2"
    local url="$3"
    local src="$MICROBE/$user/$repo";

    set -e
    debug "microbe directory: $src"
    debug "plugin directory:  $dst"
    if [ -d "$src" ]; then
        verbose -n "  - Using package from Cache ...";
        set +e
        return 0;
    fi

    verbose "  - Cloning from `yellow "$url"` ..."
    if [[ "$VERBOSE" != "no" ]]; then
        verbose ""
        git clone --depth=1 "$url" "$src"
        verbose ""
    else
        set +e
        local tmp="`mktemp`"
        git clone --depth=1 "$url" "$src" >& "$tmp";
        if [[ "$?" != "0" ]]; then
            cat "$tmp" 1>&2;
            rm "$tmp";
            exit 1;
        fi
        rm  "$tmp";
        set -e
    fi

    verbose -n "  - Adding to Pathogen Bundles ... "
    set +e
    return 0
}

function load_zip_repository() {
    local user="$1"
    local repo="$2"
    local url="$3"
    local tmp="`mktemp -d`"
    local dst="$MICROBE/$user/$repo"

    mkdir -p "$MICROBE/$user"
    verbose -n "  - Getting ZIP from `yellow "$url"` ... "
    curl -Sso "$tmp/archive.zip" -L --fail "$url"
    local st="$?"
    if [[ "$st" != "0" ]]; then error "Failed ($st)."; fi
    success "OK."

    verbose -n "  - Extracting Archive to `yellow "$dst"` ... "
    unzip "$tmp/archive" -d "$tmp" 1> /dev/null
    if [[ "$?" != "0" ]]; then error "Failed."; fi
    mv "$tmp/$repo-master" "$dst"
    rm -rf "$tmp"
    success "OK."
    verbose -n "  - Adding to Pathogen Bundles ... "
}

# --------------------------------------------------------------------------
# Actions

function action_help() {
    echo "Usage: $0 <Command> [<Parameters> ...]"
}

function action_version() {
    verbose "microbe `yellow "$VERSION"` (bash $BASH_VERSION)"
}

function action_init() {

    which curl >& /dev/null || error "Dependency missing: curl";
    which git >& /dev/null || error "Dependency missing: git";

    debug "curl:                 `which curl`"
    debug "git:                  `which git`"
    debug "microbe repository:   $MICROBE"
    debug "home path:            $HOME"
    debug "vim autoload path:    $AUTOLOAD"
    debug "pathogen bundle path: $AUTOLOAD"
    debug "pathogen path:        $PATHOGEN_VIM"

    set -e
    for dir in "$AUTOLOAD" "$BUNDLE" "$MICROBE"; do
        if [ ! -d "$dir" ]; then
            verbose -n "* Initializing '`basename "$dir"`' ... "
            mkdir -p "$dir"
            success "OK."
        fi
    done
    if [ ! -s "$PATHOGEN_VIM" ]; then
        verbose -n "* Loading Pathogen ... "
        curl -Sso "$PATHOGEN_VIM" "$PATHOGEN_REMOTE"
        success "OK."
    fi

    if [ ! -s "$HOME/.vimrc" ]; then
        verbose -n "* Creating '$HOME/.vimrc' ... "
        echo "set nocompatible" > "$HOME/.vimrc"
        echo "call pathogen#infect()" >> "$HOME/.vimrc"
        success "OK."
    else
        set +e
        grep -qx "call pathogen#infect()" "$HOME/.vimrc" >& /dev/null
        if [[ "$?" != "0" ]]; then
            set -e
            verbose -n "* Adding Pathogen to '$HOME/.vimrc' ... "
            mv "$HOME/.vimrc" "$HOME/.vimrc.bak"
            echo "set nocompatible" > "$HOME/.vimrc"
            echo "call pathogen#infect()" >> "$HOME/.vimrc"
            cat "$HOME/.vimrc.bak" >> "$HOME/.vimrc"
            success "OK."
        fi
    fi

    set +e
}

function action_install_single() {
    local spec="$1"

    if [ -z "$spec" ]; then
        echo_red "Usage: $0 install <spec>" 1>&2;
        echo "<spec> is one of:" 1>&2
        echo "- <GitHub User>/<Repository>" 1>&2
        echo "- <vim-scripts Repository>" 1>&2
        echo "- git://..." 1>&2
        exit 1;
    fi

    # Data
    local user=""
    local repo=""
    local url=""
    local resolve="yes"
    local zip="yes"

    # Git Repository?
    if [ ${spec:0:6} == "git://" ] || [[ "$spec" == *@* ]] || [ ${spec:0:8} == "https://" ]; then
        local user="external"
        local repo="`basename "$spec" ".git"`"
        local url="$spec"
        local resolve="no"
        local zip="no"

    # GitHub Repository with given User?
    else if [[ "$spec" == */* ]]; then
        IFS="/" read -r user repo <<< "$spec"
        local url="$GITHUB_HTTPS/$user/$repo"

    # GitHub Repository of vim-scripts?
    else
        local user="$DEFAULT_USER"
        local repo="$spec"
        local url="$GITHUB_HTTPS/$user/$repo"
    fi; fi

    # Initialization
    action_init

    # Repository in Cache?
    local path="$MICROBE/$user/$repo"
    if [ -d "$path" ]; then
        verbose "* Using `yellow "$user/$repo"` from Cache.";
        link_microbe_repository "$user" "$repo"
        return 0;
    else if [ -d "$path.vim" ]; then
        verbose "* Using `yellow "$user/$repo.vim"` from Cache.";
        link_microbe_repository "$user" "$repo.vim"
        return 0;
    fi; fi

    echo "* Installing `yellow "$user/$repo"` ..."

    # Resolve?
    if [[ "$resolve" == "yes" ]]; then
        verbose -n "  - Resolving Package `yellow "$repo"` ... "
        checked_url=""
        for u in "" ".vim"; do
            if check_git_repository "$url$u"; then
                checked_url="$url$u";
                repo="$repo$u";
                break;
            fi
        done
        if [ -z "$checked_url" ]; then error "Could not find Repository."; fi
        success "OK."
    else
        checked_url="$url"
    fi

    # Load Repository
    if [[ "$zip" == "yes" ]]; then
        zipUrl="$checked_url/archive/master.zip"
        load_zip_repository "$user" "$repo" "$zipUrl"
    else
        load_git_repository "$user" "$repo" "$checked_url"
    fi

    #
    link_microbe_repository "$user" "$repo"

    # Delete everything that is not needed
    set -e
    for x in `find "$MICROBE/$user/$repo" -mindepth 1 -maxdepth 1 2> /dev/null`;
    do
        local n="`basename "$x"`"
        case "$n" in
            syntax|indent|autoload|colors|ftplugin|*.vim|doc)
                ;;
            *)
                rm -r "$x"
                ;;
        esac
    done

    # Write Metadata
    echo "$spec" > "$MICROBE/$user/$repo/.microbe_spec"
    success "OK."
    set +e
}

function action_install() {
    while [ $# -gt 0 ]; do
        action_install_single "$1"
        shift
    done
}

function action_remove_single() {
    local spec="$1"
    IFS="/" read -r user pckg <<< "$spec"

    # Check
    if [ -z "$user" -a -z "$pckg" ]; then
        error "Usage: $0 install [<GitHub User>/]<Package>"
    fi

    # Fallback to Default User?
    if [ -z "$pckg" ]; then
        pckg="$user"
        user="$DEFAULT_USER"
    fi

    # Remove
    set -e
    for r in "$pckg" "$pckg.vim";
    do
        path="$MICROBE/$user/$r"
        if [ -d "$path" ]; then
            verbose -n "* Removing `yellow "$user/$r"` ... "
            if [ -L "$BUNDLE/${user}_${r}" ]; then rm "$BUNDLE/${user}_${r}"; fi
            success "OK."

        fi
    done
    set +e
}

function action_remove() {
    while [ $# -gt 0 ]; do
        action_remove_single "$1"
        shift
    done
}

function action_purge_single() {
    local spec="$1"
    IFS="/" read -r user pckg <<< "$spec"

    # Check
    if [ -z "$user" -a -z "$pckg" ]; then
        error "Usage: $0 install [<GitHub User>] <Package>"
    fi

    # Fallback to Default User?
    if [ -z "$pckg" ]; then
        pckg="$user"
        user="$DEFAULT_USER"
    fi

    # Remove
    set -e
    for r in "$pckg" "$pckg.vim";
    do
        path="$MICROBE/$user/$r"
        if [ -d "$path" ]; then
            verbose -n "* Purging `yellow "$user/$r"` ... "
            rm -rf "$path"
            if [ -L "$BUNDLE/${user}_${r}" ]; then rm "$BUNDLE/${user}_${r}"; fi
            success "OK."

        fi
    done
    set +e
}

function action_purge() {
    while [ $# -gt 0 ]; do
        action_purge_single "$1"
        shift
    done
}

function action_update_single() {
    local specFile="$1"
    local repoDir="`dirname "$specFile"`"
    local repo="`basename "$repoDir"`"
    local userDir="`dirname "$repoDir"`"
    local user="`basename "$userDir"`"

    verbose -n "* Updating `yellow "$user/$repo"` ... "
    if [ -d "$repoDir/.git" ]; then
        cd "$repoDir"
        git pull -q
    else
        local spec="`cat "$specFile"`"
        local installed="yes"
        if [ ! -L "$BUNDLE/${user}_${repo}" ]; then installed="no"; fi
        action_purge "$user/$repo" >& /dev/null
        action_install "$spec" 1> /dev/null
        if [ "$installed" == "no" ]; then rm "$BUNDLE/${user}_${repo}"; fi
    fi
    success "OK."
}

function action_update() {

    # Update all?
    if [ "$#" == "0" ]; then
        set -e
        for f in `find "$MICROBE" -mindepth 3 -maxdepth 3 -type f -name ".microbe_spec" 2> /dev/null | sort`; do
            action_update_single "$f"
        done
        set +e
    else
        while [ $# -gt 0 ]; do
            local spec="$1"
            IFS="/" read -r user pckg <<< "$spec"

            # Fallback to Default User?
            if [ -z "$pckg" ]; then
                pckg="$user"
                user="$DEFAULT_USER"
            fi

            # Update
            set -e
            local path="$MICROBE/$user/$pckg"
            if [ ! -e "$path/.microbe_spec" ]; then path="$path.vim"; fi
            if [ ! -e "$path/.microbe_spec" ]; then shift; continue; fi
            action_update_single "$path/.microbe_spec"
            set +e
            shift
        done
    fi
}

function action_update_pathogen() {
    if [ ! -s "$PATHOGEN_VIM" ]; then
        action_init;
    else
        verbose -n "* Updating Pathogen ... "
        mv "$PATHOGEN_VIM" "$PATHOGEN_VIM.bak"
        curl -Sso "$PATHOGEN_VIM" "$PATHOGEN_REMOTE"
        if [[ "$?" != "0" ]]; then
            error "Could not download Pathogen."
            mv "$PATHOGEN_VIM.bak" "$PATHOGEN_VIM"
        else
            rm "$PATHOGEN_VIM.bak"
            success "OK."
        fi
    fi
}

function action_list() {
    set -e
    for dir in `find "$MICROBE" -mindepth 3 -maxdepth 3 -type f -name ".microbe_spec" 2> /dev/null | sort`; do
        local repoDir="`dirname "$dir"`"
        local repo="`basename "$repoDir"`"
        local userDir="`dirname "$repoDir"`"
        local user="`basename "$userDir"`"

        local s="`du -sk "$repoDir"`"
        local kb=""
        local r=""
        read kb r <<< "$s"

        local installed="(not installed)"
        if [ -L "$BUNDLE/${user}_${repo}" ]; then local installed="    `green "(installed)"`"; fi
        local line="$(printf "%50s     %s     %6s    %s" "$user/`yellow "$repo"`" "$installed" "${kb}KB" "$repoDir")"

        verbose "$line"
    done
    set +e
}

# --------------------------------------------------------------------------
# Handlers
if [ -z "$COMMAND" ] || [[ "$COMMAND" == "help" ]]; then
    action_help
    exit 0;
fi

shift
case "$COMMAND" in
    "version")
        action_version
        ;;
    "init")
        action_init
        ;;
    "install")
        action_install "$@"
        ;;
    "remove")
        action_remove "$@"
        ;;
    "purge")
        action_purge "$@"
        ;;
    "update")
        action_update "$@"
        ;;
    "update-pathogen")
        action_update_pathogen
        ;;
    "list")
        action_list
        ;;
    *)
        error "Unknown Action: $COMMAND";
        ;;
esac


Added boilerplate shell script in debug

Added boilerplate shell script in debug

error: pathspec 'debug/vmware-apis.sh' did not match any file(s) known to git


Add batch launcher with argument

Add batch launcher with argument

#!/bin/bash -x

echo "$( basename ${0} ) can be executed with the -x (debug) option, /bin/bash has been found on this system and used to execute this script"
echo "bash version:"
/bin/bash --version

error: pathspec 'batch-launcher/absolute-path-launcher-with-argument.sh' did not match any file(s) known to git


FIX: v1 not accessible if reverse proxy

FIX: v1 not accessible if reverse proxy

curl attempting to connect to the cattle url should follow redirection if rancher is under a reverse_proxy, adding -L to curl command

#!/bin/bash
set -e

trap "exit 1" SIGINT SIGTERM

export AGENT_CONF_FILE="/var/lib/rancher/etc/agent.conf"
export CA_CERT_FILE="/var/lib/rancher/etc/ssl/ca.crt"

# This is copied from common/scripts.sh, if there is a change here
# make it in common and then copy here
check_debug()
{
    if [ -n "$CATTLE_SCRIPT_DEBUG" ] || echo "${@}" | grep -q -- --debug; then
        export CATTLE_SCRIPT_DEBUG=true
        export PS4='[${BASH_SOURCE##*/}:${LINENO}] '
        set -x
    fi
}

info()
{
    echo "INFO:" "${@}"
}

error()
{
    echo "ERROR:" "${@}" 1>&2
}

export CATTLE_HOME=${CATTLE_HOME:-/var/lib/cattle}
# End copy

check_debug

CONTAINER="$(hostname)"
if [ "$1" = "run" ]; then
    CONTAINER="rancher-agent"
fi

if [[ "$1" != "inspect-host" && $1 != "--" && "$1" != "state" ]]; then
    RUNNING_IMAGE="$(docker inspect -f '{{.Config.Image}}' ${CONTAINER})"
fi

if [[ -n ${RUNNING_IMAGE} && ${RUNNING_IMAGE} != ${RANCHER_AGENT_IMAGE} ]]; then
    export RANCHER_AGENT_IMAGE=${RUNNING_IMAGE}
fi

check_and_add_conf()
{
    if [ -d $(dirname ${AGENT_CONF_FILE}) ]; then
        touch ${AGENT_CONF_FILE}
        grep -q -F "${1}=${2}" ${AGENT_CONF_FILE} || \
            echo "export ${1}=${2}" >> ${AGENT_CONF_FILE}
    fi
}

setup_custom_ca_bundle()
{
    check_and_add_conf "CURL_CA_BUNDLE" ${CA_CERT_FILE}
    check_and_add_conf "REQUESTS_CA_BUNDLE" ${CA_CERT_FILE}

    # Update core container CA certs for Golang
    mkdir -p /usr/local/share/ca-certificates/rancher
    cp ${CA_CERT_FILE} /usr/local/share/ca-certificates/rancher/rancherAddedCA.crt
    update-ca-certificates

    # Configure python websocket pre-shipped cacerts.
    local websocket_pem='/var/lib/cattle/pyagent/dist/websocket/cacert.pem'
    local websocket_orig='/var/lib/cattle/pyagent/dist/websocket/cacert.orig'
    if [[ -e ${websocket_pem} ]]; then
        if [[ ! -e ${websocket_orig} ]]; then
            cp ${websocket_pem} ${websocket_orig}
        fi
        cat ${websocket_orig} ${CA_CERT_FILE} > ${websocket_pem}
    fi
}

if [ -e ${CA_CERT_FILE} ]; then
    setup_custom_ca_bundle
fi

if [ -e "${AGENT_CONF_FILE}" ]; then
    source "${AGENT_CONF_FILE}"
fi

launch_volume()
{
    if docker inspect rancher-agent-state >/dev/null 2>&1; then
        return
    fi

    local opts=""

    if [ "${CATTLE_VAR_LIB_WRITABLE}" = "true" ]; then
        opts="-v /var/lib/rancher:/var/lib/rancher"
    else
        opts="-v /var/lib/rancher"
    fi

    docker run \
        --name rancher-agent-state \
        -v /var/lib/cattle \
        -v /var/log/rancher:/var/log/rancher \
        ${opts} ${RANCHER_AGENT_IMAGE} state
}

inspect_host()
{
    docker run --rm --privileged -v /run:/run -v /var/lib:/var/lib ${RANCHER_AGENT_IMAGE} inspect-host
}

launch_agent()
{
    launch_volume

    # Needed to edit resolv.conf on the host
    local var_lib_docker=$(resolve_var_lib_docker)

    if [ -n "$NO_PROXY" ]; then
        export no_proxy=$NO_PROXY
    fi

    docker run \
        -d \
        --name rancher-agent \
        --restart=always \
        --net=host \
        --pid=host \
        --privileged \
        -e CATTLE_AGENT_PIDNS=host \
        -e http_proxy \
        -e https_proxy \
        -e NO_PROXY \
        -e no_proxy \
        -e CATTLE_PHYSICAL_HOST_UUID \
        -e CATTLE_SCRIPT_DEBUG \
        -e CATTLE_ACCESS_KEY \
        -e CATTLE_SECRET_KEY \
        -e CATTLE_AGENT_IP \
        -e CATTLE_HOST_API_PROXY \
        -e CATTLE_SYSTEMD \
        -e CATTLE_URL \
        -e CATTLE_HOST_LABELS \
        -e CATTLE_VOLMGR_ENABLED \
        -e HOST_DOCKER_SOCK \
        -v $HOST_DOCKER_SOCK:/var/run/docker.sock \
        -v /lib/modules:/lib/modules:ro \
        -v ${var_lib_docker}:${var_lib_docker} \
        -v /proc:/host/proc \
        -v /dev:/host/dev \
        --volumes-from rancher-agent-state \
        "${RANCHER_AGENT_IMAGE}" "$@"
}

resolve_var_lib_docker()
{
    local dir="$(docker inspect -f '{{index .Volumes "/var/lib/cattle"}}' rancher-agent-state)"
    echo $(dirname $(dirname $(dirname $dir)))
}

verify_docker_client_server_version()
{
    local client_version=$(docker version |grep Client\ version | cut -d":" -f2)
    info "Checking for Docker version >=" $client_version
    docker version 2>&1 | grep Server\ version >/dev/null || {
        echo "Please ensure Host Docker version is >=${client_version} and container has r/w permissions to docker.sock" 1>&2
        exit 1
    }
    info Found $(docker version 2>&1 | grep Server\ version)
    for i in version info; do
        docker $i | while read LINE; do
            info "docker $i:" $LINE
        done
    done
}

delete_container()
{
    while docker inspect $1 >/dev/null 2>&1; do
        info Deleting container $1
        docker rm -f $1 >/dev/null 2>&1 || true
    done
}

cleanup_agent()
{
    delete_container rancher-agent
}

cleanup_upgrade()
{
    delete_container rancher-agent-upgrade
}

setup_state()
{
    mkdir -p /var/lib/{cattle,rancher/state}

    export CATTLE_STATE_DIR=/var/lib/rancher/state
    export CATTLE_AGENT_LOG_FILE=/var/log/rancher/agent.log
    export CATTLE_CADVISOR_WRAPPER=cadvisor.sh

    if [ "$CATTLE_SYSTEMD" = "true" ]; then
        mkdir -p /run/systemd/system
    fi
}

load()
{
    local content=$(curl -sL $1)

    if [[ "$content" =~ .!/bin/sh.* ]]; then
        eval "$content"
        if [ -n "$CATTLE_URL_OVERRIDE" ]; then
            CATTLE_URL=$CATTLE_URL_OVERRIDE
        fi
    else
        error $(print_url $1) returned
        error "--- START ---"
        echo "$content"
        error "--- END ---"
        return 1
    fi
}

print_token()
{
    local token_file=/var/lib/rancher/state/.registration_token
    local token=

    if [ -e $token_file ]; then
        token="$(<$token_file)"
    fi

    if [ -z "$token" ]; then
        token=$(openssl rand -hex 64)
        mkdir -p $(dirname $token_file)
        echo $token > $token_file
    fi

    info env "TOKEN=$token"
}

register()
{
    ENV=$(./register.py $TOKEN)
    eval "$ENV"

    export CATTLE_AGENT_IP=${CATTLE_AGENT_IP:-${DETECTED_CATTLE_AGENT_IP}}
}

run_bootstrap()
{
    SCRIPT=/tmp/bootstrap.sh
    touch $SCRIPT
    chmod 700 $SCRIPT

    export CATTLE_CONFIG_URL="${CATTLE_CONFIG_URL:-${CATTLE_URL}}"
    export CATTLE_STORAGE_URL="${CATTLE_STORAGE_URL:-${CATTLE_URL}}"

    curl -u ${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY} -s ${CATTLE_URL}/scripts/bootstrap > $SCRIPT

    # Sanity check if this account is really being authenticated as an agent account or the default admin auth
    if curl -f -u ${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY} -s ${CATTLE_URL}/schemas/account >/dev/null 2>&1; then
        error Please re-register this agent
        exit 1
    fi

    info "Starting agent for ${CATTLE_ACCESS_KEY}"
    if [ "$CATTLE_EXEC_AGENT" = "true" ]; then
        exec bash $SCRIPT "$@"
    else
        bash $SCRIPT "$@"
    fi
}

run()
{
    mount --rbind /host/dev /dev
    while true; do
        run_bootstrap "$@" || true
        sleep 5
    done
}

read_rancher_agent_env()
{
    info Reading environment from rancher-agent
    local save=$RANCHER_AGENT_IMAGE
    eval $(docker inspect rancher-agent | jq -r '"export \"" + .[0].Config.Env[] + "\""')
    RANCHER_AGENT_IMAGE=$save
}

print_url()
{
    local url=$(echo "$1"| sed -e 's!/v1/scripts.*!/v1!')
    echo $url
}

wait_for()
{
    local url="$(print_url $CATTLE_URL)"
    info "Attempting to connect to: ${url}"
    for ((i=0; i < 300; i++)); do
        if ! curl -f -L -s ${CATTLE_URL} >/dev/null 2>&1; then
            error ${url} is not accessible
            sleep 2
            if [ "$i" -eq "299" ]; then
                error "Could not reach ${url}. Giving up."
                exit 1
            fi
        else
            info "${url} is accessible"
            break
        fi
    done
}

inspect()
{
    print_token

    if mkdir -p /var/lib/rancher/state >/dev/null 2>&1; then
        info env "CATTLE_VAR_LIB_WRITABLE=true"
    else
        info env "CATTLE_VAR_LIB_WRITABLE=false"
    fi

    if [ -e /var/run/system-docker.sock ]; then
        info env "CATTLE_RANCHEROS=true"
    else
        info env "CATTLE_RANCHEROS=false"
    fi

    if [ -e /run/systemd/system ]; then
        info env "CATTLE_SYSTEMD=true"
    else
        info env "CATTLE_SYSTEMD=false"
    fi
}

setup_env()
{
    if [ "$1" != "upgrade" ]; then
        local env="$(./resolve_url.py $CATTLE_URL)"
        if ! load "$env"; then
            error Failed to load registration env from CATTLE_URL=$(print_url $CATTLE_URL) ENV_URL=$(print_url $env)
            error Please ensure the proper value for the Host Registration URL is set
            exit 1
        fi
    fi

    info Inspecting host capabilities
    local content=$(inspect_host)

    echo "$content" | grep -v 'INFO: env' || true
    eval $(echo "$content" | grep 'INFO: env' | sed 's/INFO: env//g')

    export CATTLE_SYSTEMD

    info System: ${CATTLE_SYSTEMD}
    info Host writable: ${CATTLE_VAR_LIB_WRITABLE}
    info Token: $(echo $TOKEN | sed 's/........*/xxxxxxxx/g')

    if [[ -z "$CATTLE_ACCESS_KEY" || -z "$CATTLE_SECRET_KEY" ]]; then
        info Running registration
        register
    else
        info Skipping registration
    fi

    info Printing Environment
    env | sort | while read LINE; do
        if [[ $LINE =~ RANCHER.* || $LINE =~ CATTLE.* ]]; then
            info "ENV:" $(echo $LINE | sed 's/\(SECRET.*=\).*/\1xxxxxxx/g')
        fi
    done
}

setup_cattle_url()
{
    if [ "$1" = "register" ]; then
        if [ -z "$RANCHER_URL" ]; then
            info No RANCHER_URL environment variable, exiting
            exit 0
        fi
        CATTLE_URL="$RANCHER_URL"
    elif [ "$1" = "upgrade" ]; then
        read_rancher_agent_env
    else
        CATTLE_URL="$1"
    fi

    if echo $CATTLE_URL | grep -qE '127.0.0.1|localhost'; then
        local gateway=$(docker run --rm --net=host $RANCHER_AGENT_IMAGE -- ip route get 8.8.8.8 | grep via | awk '{print $7}')
        CATTLE_URL=$(echo $CATTLE_URL | sed -e 's/127.0.0.1/'$gateway'/' -e 's/localhost/'$gateway'/')
    fi

    export CATTLE_URL
}

if [ "$#" == 0 ]; then
    error "One parameter required"
    exit 1
fi

if [[ $1 =~ http.* || $1 = "register" || $1 = "upgrade" ]]; then
    echo $http_proxy $https_proxy
    setup_cattle_url $1
    if [ "$1" = "upgrade" ]; then
        info Running upgrade
    else
        info Running Agent Registration Process, CATTLE_URL=$(print_url $CATTLE_URL)
    fi
    verify_docker_client_server_version
    if [ "$1" != "upgrade" ]; then
        wait_for
    fi
    setup_env $1
    cleanup_agent
    ID=$(launch_agent run)
    info Launched Rancher Agent: $ID
elif [ "$1" = "inspect-host" ]; then
    inspect
elif [ "$1" = "state" ]; then
    echo Rancher State
elif [ "$1" = "run" ]; then
    cleanup_upgrade
    setup_state
    run
elif [ "$1" = "--" ]; then
    shift 1
    exec "$@"
fi

#!/bin/bash
set -e

trap "exit 1" SIGINT SIGTERM

export AGENT_CONF_FILE="/var/lib/rancher/etc/agent.conf"
export CA_CERT_FILE="/var/lib/rancher/etc/ssl/ca.crt"

# This is copied from common/scripts.sh, if there is a change here
# make it in common and then copy here
check_debug()
{
    if [ -n "$CATTLE_SCRIPT_DEBUG" ] || echo "${@}" | grep -q -- --debug; then
        export CATTLE_SCRIPT_DEBUG=true
        export PS4='[${BASH_SOURCE##*/}:${LINENO}] '
        set -x
    fi
}

info()
{
    echo "INFO:" "${@}"
}

error()
{
    echo "ERROR:" "${@}" 1>&2
}

export CATTLE_HOME=${CATTLE_HOME:-/var/lib/cattle}
# End copy

check_debug

CONTAINER="$(hostname)"
if [ "$1" = "run" ]; then
    CONTAINER="rancher-agent"
fi

if [[ "$1" != "inspect-host" && $1 != "--" && "$1" != "state" ]]; then
    RUNNING_IMAGE="$(docker inspect -f '{{.Config.Image}}' ${CONTAINER})"
fi

if [[ -n ${RUNNING_IMAGE} && ${RUNNING_IMAGE} != ${RANCHER_AGENT_IMAGE} ]]; then
    export RANCHER_AGENT_IMAGE=${RUNNING_IMAGE}
fi

check_and_add_conf()
{
    if [ -d $(dirname ${AGENT_CONF_FILE}) ]; then
        touch ${AGENT_CONF_FILE}
        grep -q -F "${1}=${2}" ${AGENT_CONF_FILE} || \
            echo "export ${1}=${2}" >> ${AGENT_CONF_FILE}
    fi
}

setup_custom_ca_bundle()
{
    check_and_add_conf "CURL_CA_BUNDLE" ${CA_CERT_FILE}
    check_and_add_conf "REQUESTS_CA_BUNDLE" ${CA_CERT_FILE}

    # Update core container CA certs for Golang
    mkdir -p /usr/local/share/ca-certificates/rancher
    cp ${CA_CERT_FILE} /usr/local/share/ca-certificates/rancher/rancherAddedCA.crt
    update-ca-certificates

    # Configure python websocket pre-shipped cacerts.
    local websocket_pem='/var/lib/cattle/pyagent/dist/websocket/cacert.pem'
    local websocket_orig='/var/lib/cattle/pyagent/dist/websocket/cacert.orig'
    if [[ -e ${websocket_pem} ]]; then
        if [[ ! -e ${websocket_orig} ]]; then
            cp ${websocket_pem} ${websocket_orig}
        fi
        cat ${websocket_orig} ${CA_CERT_FILE} > ${websocket_pem}
    fi
}

if [ -e ${CA_CERT_FILE} ]; then
    setup_custom_ca_bundle
fi

if [ -e "${AGENT_CONF_FILE}" ]; then
    source "${AGENT_CONF_FILE}"
fi

launch_volume()
{
    if docker inspect rancher-agent-state >/dev/null 2>&1; then
        return
    fi

    local opts=""

    if [ "${CATTLE_VAR_LIB_WRITABLE}" = "true" ]; then
        opts="-v /var/lib/rancher:/var/lib/rancher"
    else
        opts="-v /var/lib/rancher"
    fi

    docker run \
        --name rancher-agent-state \
        -v /var/lib/cattle \
        -v /var/log/rancher:/var/log/rancher \
        ${opts} ${RANCHER_AGENT_IMAGE} state
}

inspect_host()
{
    docker run --rm --privileged -v /run:/run -v /var/lib:/var/lib ${RANCHER_AGENT_IMAGE} inspect-host
}

launch_agent()
{
    launch_volume

    # Needed to edit resolv.conf on the host
    local var_lib_docker=$(resolve_var_lib_docker)

    if [ -n "$NO_PROXY" ]; then
        export no_proxy=$NO_PROXY
    fi

    docker run \
        -d \
        --name rancher-agent \
        --restart=always \
        --net=host \
        --pid=host \
        --privileged \
        -e CATTLE_AGENT_PIDNS=host \
        -e http_proxy \
        -e https_proxy \
        -e NO_PROXY \
        -e no_proxy \
        -e CATTLE_PHYSICAL_HOST_UUID \
        -e CATTLE_SCRIPT_DEBUG \
        -e CATTLE_ACCESS_KEY \
        -e CATTLE_SECRET_KEY \
        -e CATTLE_AGENT_IP \
        -e CATTLE_HOST_API_PROXY \
        -e CATTLE_SYSTEMD \
        -e CATTLE_URL \
        -e CATTLE_HOST_LABELS \
        -e CATTLE_VOLMGR_ENABLED \
        -e HOST_DOCKER_SOCK \
        -v $HOST_DOCKER_SOCK:/var/run/docker.sock \
        -v /lib/modules:/lib/modules:ro \
        -v ${var_lib_docker}:${var_lib_docker} \
        -v /proc:/host/proc \
        -v /dev:/host/dev \
        --volumes-from rancher-agent-state \
        "${RANCHER_AGENT_IMAGE}" "$@"
}

resolve_var_lib_docker()
{
    local dir="$(docker inspect -f '{{index .Volumes "/var/lib/cattle"}}' rancher-agent-state)"
    echo $(dirname $(dirname $(dirname $dir)))
}

verify_docker_client_server_version()
{
    local client_version=$(docker version |grep Client\ version | cut -d":" -f2)
    info "Checking for Docker version >=" $client_version
    docker version 2>&1 | grep Server\ version >/dev/null || {
        echo "Please ensure Host Docker version is >=${client_version} and container has r/w permissions to docker.sock" 1>&2
        exit 1
    }
    info Found $(docker version 2>&1 | grep Server\ version)
    for i in version info; do
        docker $i | while read LINE; do
            info "docker $i:" $LINE
        done
    done
}

delete_container()
{
    while docker inspect $1 >/dev/null 2>&1; do
        info Deleting container $1
        docker rm -f $1 >/dev/null 2>&1 || true
    done
}

cleanup_agent()
{
    delete_container rancher-agent
}

cleanup_upgrade()
{
    delete_container rancher-agent-upgrade
}

setup_state()
{
    mkdir -p /var/lib/{cattle,rancher/state}

    export CATTLE_STATE_DIR=/var/lib/rancher/state
    export CATTLE_AGENT_LOG_FILE=/var/log/rancher/agent.log
    export CATTLE_CADVISOR_WRAPPER=cadvisor.sh

    if [ "$CATTLE_SYSTEMD" = "true" ]; then
        mkdir -p /run/systemd/system
    fi
}

load()
{
    local content=$(curl -sL $1)

    if [[ "$content" =~ .!/bin/sh.* ]]; then
        eval "$content"
        if [ -n "$CATTLE_URL_OVERRIDE" ]; then
            CATTLE_URL=$CATTLE_URL_OVERRIDE
        fi
    else
        error $(print_url $1) returned
        error "--- START ---"
        echo "$content"
        error "--- END ---"
        return 1
    fi
}

print_token()
{
    local token_file=/var/lib/rancher/state/.registration_token
    local token=

    if [ -e $token_file ]; then
        token="$(<$token_file)"
    fi

    if [ -z "$token" ]; then
        token=$(openssl rand -hex 64)
        mkdir -p $(dirname $token_file)
        echo $token > $token_file
    fi

    info env "TOKEN=$token"
}

register()
{
    ENV=$(./register.py $TOKEN)
    eval "$ENV"

    export CATTLE_AGENT_IP=${CATTLE_AGENT_IP:-${DETECTED_CATTLE_AGENT_IP}}
}

run_bootstrap()
{
    SCRIPT=/tmp/bootstrap.sh
    touch $SCRIPT
    chmod 700 $SCRIPT

    export CATTLE_CONFIG_URL="${CATTLE_CONFIG_URL:-${CATTLE_URL}}"
    export CATTLE_STORAGE_URL="${CATTLE_STORAGE_URL:-${CATTLE_URL}}"

    curl -u ${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY} -s ${CATTLE_URL}/scripts/bootstrap > $SCRIPT

    # Sanity check if this account is really being authenticated as an agent account or the default admin auth
    if curl -f -u ${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY} -s ${CATTLE_URL}/schemas/account >/dev/null 2>&1; then
        error Please re-register this agent
        exit 1
    fi

    info "Starting agent for ${CATTLE_ACCESS_KEY}"
    if [ "$CATTLE_EXEC_AGENT" = "true" ]; then
        exec bash $SCRIPT "$@"
    else
        bash $SCRIPT "$@"
    fi
}

run()
{
    mount --rbind /host/dev /dev
    while true; do
        run_bootstrap "$@" || true
        sleep 5
    done
}

read_rancher_agent_env()
{
    info Reading environment from rancher-agent
    local save=$RANCHER_AGENT_IMAGE
    eval $(docker inspect rancher-agent | jq -r '"export \"" + .[0].Config.Env[] + "\""')
    RANCHER_AGENT_IMAGE=$save
}

print_url()
{
    local url=$(echo "$1"| sed -e 's!/v1/scripts.*!/v1!')
    echo $url
}

wait_for()
{
    local url="$(print_url $CATTLE_URL)"
    info "Attempting to connect to: ${url}"
    for ((i=0; i < 300; i++)); do
        if ! curl -f -s ${CATTLE_URL} >/dev/null 2>&1; then
            error ${url} is not accessible
            sleep 2
            if [ "$i" -eq "299" ]; then
                error "Could not reach ${url}. Giving up."
                exit 1
            fi
        else
            info "${url} is accessible"
            break
        fi
    done
}

inspect()
{
    print_token

    if mkdir -p /var/lib/rancher/state >/dev/null 2>&1; then
        info env "CATTLE_VAR_LIB_WRITABLE=true"
    else
        info env "CATTLE_VAR_LIB_WRITABLE=false"
    fi

    if [ -e /var/run/system-docker.sock ]; then
        info env "CATTLE_RANCHEROS=true"
    else
        info env "CATTLE_RANCHEROS=false"
    fi

    if [ -e /run/systemd/system ]; then
        info env "CATTLE_SYSTEMD=true"
    else
        info env "CATTLE_SYSTEMD=false"
    fi
}

setup_env()
{
    if [ "$1" != "upgrade" ]; then
        local env="$(./resolve_url.py $CATTLE_URL)"
        if ! load "$env"; then
            error Failed to load registration env from CATTLE_URL=$(print_url $CATTLE_URL) ENV_URL=$(print_url $env)
            error Please ensure the proper value for the Host Registration URL is set
            exit 1
        fi
    fi

    info Inspecting host capabilities
    local content=$(inspect_host)

    echo "$content" | grep -v 'INFO: env' || true
    eval $(echo "$content" | grep 'INFO: env' | sed 's/INFO: env//g')

    export CATTLE_SYSTEMD

    info System: ${CATTLE_SYSTEMD}
    info Host writable: ${CATTLE_VAR_LIB_WRITABLE}
    info Token: $(echo $TOKEN | sed 's/........*/xxxxxxxx/g')

    if [[ -z "$CATTLE_ACCESS_KEY" || -z "$CATTLE_SECRET_KEY" ]]; then
        info Running registration
        register
    else
        info Skipping registration
    fi

    info Printing Environment
    env | sort | while read LINE; do
        if [[ $LINE =~ RANCHER.* || $LINE =~ CATTLE.* ]]; then
            info "ENV:" $(echo $LINE | sed 's/\(SECRET.*=\).*/\1xxxxxxx/g')
        fi
    done
}

setup_cattle_url()
{
    if [ "$1" = "register" ]; then
        if [ -z "$RANCHER_URL" ]; then
            info No RANCHER_URL environment variable, exiting
            exit 0
        fi
        CATTLE_URL="$RANCHER_URL"
    elif [ "$1" = "upgrade" ]; then
        read_rancher_agent_env
    else
        CATTLE_URL="$1"
    fi

    if echo $CATTLE_URL | grep -qE '127.0.0.1|localhost'; then
        local gateway=$(docker run --rm --net=host $RANCHER_AGENT_IMAGE -- ip route get 8.8.8.8 | grep via | awk '{print $7}')
        CATTLE_URL=$(echo $CATTLE_URL | sed -e 's/127.0.0.1/'$gateway'/' -e 's/localhost/'$gateway'/')
    fi

    export CATTLE_URL
}

if [ "$#" == 0 ]; then
    error "One parameter required"
    exit 1
fi

if [[ $1 =~ http.* || $1 = "register" || $1 = "upgrade" ]]; then
    echo $http_proxy $https_proxy
    setup_cattle_url $1
    if [ "$1" = "upgrade" ]; then
        info Running upgrade
    else
        info Running Agent Registration Process, CATTLE_URL=$(print_url $CATTLE_URL)
    fi
    verify_docker_client_server_version
    if [ "$1" != "upgrade" ]; then
        wait_for
    fi
    setup_env $1
    cleanup_agent
    ID=$(launch_agent run)
    info Launched Rancher Agent: $ID
elif [ "$1" = "inspect-host" ]; then
    inspect
elif [ "$1" = "state" ]; then
    echo Rancher State
elif [ "$1" = "run" ]; then
    cleanup_upgrade
    setup_state
    run
elif [ "$1" = "--" ]; then
    shift 1
    exec "$@"
fi


Changing to do a checksum-based

Changing to do a checksum-based

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/enckse/core-scripts.git/'


Add alias for shell rc file reloading

Add alias for shell rc file reloading

if [ ${SHELL_NAME} ="zsh" ]; then
    alias reload="source ~/.zshrc"
else
    alias reload="source ~/.bashrc"
fi

error: pathspec 'alias/dev.sh' did not match any file(s) known to git


Update prompt to work on Mac as well

Update prompt to work on Mac as well

# autoload colors && colors
# cheers, @ehrenmurdick
# http://github.com/ehrenmurdick/config/blob/master/zsh/prompt.zsh

if (( $+commands[git] ))
then
  git="$commands[git]"
else
  git="/usr/bin/git"
fi

git_branch() {
  echo $($git symbolic-ref HEAD 2>/dev/null | awk -F/ {'print $NF'})
}

git_dirty() {
  st=$($git status 2>/dev/null | tail -n 1)
  if [[ $st == "" ]]
  then
    echo ""
  else
    if [[ "$st" =~ ^nothing ]]
    then
      echo "on %{$fg_bold[green]%}$(git_prompt_info)%{$reset_color%}"
    else
      echo "on %{$fg_bold[red]%}$(git_prompt_info)%{$reset_color%}"
    fi
  fi
}

git_prompt_info () {
 ref=$($git symbolic-ref HEAD 2>/dev/null) || return
# echo "(%{\e[0;33m%}${ref#refs/heads/}%{\e[0m%})"
 echo "${ref#refs/heads/}"
}

commit_status() {
  status_line=$(git status -sb --porcelain 2>/dev/null | cut -d' ' -f3,4,5,6 | grep '\[' | sed 's/[^a-z 0-9,]*//g')
  if ! [[ -z "$status_line" ]]
  then
    echo " ($status_line)"
  else
    echo ""
  fi
}

ruby_version() {
  if (( $+commands[rbenv] ))
  then
    echo "$(rbenv version | awk '{print $1}')"
  fi

  if (( $+commands[rvm-prompt] ))
  then
    echo "$(rvm-prompt | awk '{print $1}')"
  fi
}

rb_prompt() {
  if ! [[ -z "$(ruby_version)" ]]
  then
    echo "%{$fg_bold[yellow]%}$(ruby_version)%{$reset_color%}"
  else
    echo ""
  fi
}

directory_name() {
  echo "%{$fg_bold[cyan]%}%1/%\/%{$reset_color%}"
}

running_jobs() {
  job_count=$(jobs | grep '^\[' | wc -l | awk '{print $1}')
  echo "%{$fg_bold[magenta]%}[$job_count]%{$reset_color%}"
}

cpu_util() {
  if [[ "$(uname -a | awk '{print $1}')" =~ ^Darwin ]]
  then
    echo ""
  else
    utilization=$(iostat -c | grep -v '^[a-zA-Z]' | grep '[0-9]' | awk '{print int($1+0.5)}')
    color=blue

    if [ $utilization -gt 60 ]
    then
      color=red
    fi
    echo "%{$fg_bold[$color]%}[$utilization]%{$reset_color%}"
  fi
}

prompt_status_bar(){
  timestamp=$(date +'%r')
  echo "%{$fg_bold[grey]%}$timestamp $(running_jobs)$(cpu_util)%{$reset_color%}"
}

TRAPALRM() {
      zle reset-prompt
}

export PROMPT=$'\n$(prompt_status_bar) $(rb_prompt) in $(directory_name) $(git_dirty)$(commit_status)\n› '
set_prompt () {
  export RPROMPT="%{$fg_bold[cyan]%}%{$reset_color%}"
  TMOUT=1
}

precmd() {
  title "zsh" "%m" "%55<...<%~"
  set_prompt
}

# autoload colors && colors
# cheers, @ehrenmurdick
# http://github.com/ehrenmurdick/config/blob/master/zsh/prompt.zsh

if (( $+commands[git] ))
then
  git="$commands[git]"
else
  git="/usr/bin/git"
fi

git_branch() {
  echo $($git symbolic-ref HEAD 2>/dev/null | awk -F/ {'print $NF'})
}

git_dirty() {
  st=$($git status 2>/dev/null | tail -n 1)
  if [[ $st == "" ]]
  then
    echo ""
  else
    if [[ "$st" =~ ^nothing ]]
    then
      echo "on %{$fg_bold[green]%}$(git_prompt_info)%{$reset_color%}"
    else
      echo "on %{$fg_bold[red]%}$(git_prompt_info)%{$reset_color%}"
    fi
  fi
}

git_prompt_info () {
 ref=$($git symbolic-ref HEAD 2>/dev/null) || return
# echo "(%{\e[0;33m%}${ref#refs/heads/}%{\e[0m%})"
 echo "${ref#refs/heads/}"
}

unpushed () {
  $git cherry -v @{upstream} 2>/dev/null | wc -l
}

commits_behind() {
  $git rev-list ...@ >/dev/null 2>&1 | wc -l
}

need_push () {
  local commits=$(unpushed)
  if [[ $commits == 0 ]]
  then
   echo "0"
  else
    echo "%{$fg_bold[magenta]%}+%{$reset_color%}$commits"
  fi
}

needs_sync () {
  local commits=$(commits_behind)
  if [[ $commits == 0 ]]
  then
   echo "0"
  else
    echo "%{$fg_bold[magenta]%}-%{$reset_color%}$commits"
  fi
}

commit_status() {
  local status_line="($(needs_sync)/$(need_push))"
  if [[ $status_line == "(0/0)" ]]
  then
    echo ""
  else
    echo " $status_line"
  fi
}

ruby_version() {
  if (( $+commands[rbenv] ))
  then
    echo "$(rbenv version | awk '{print $1}')"
  fi

  if (( $+commands[rvm-prompt] ))
  then
    echo "$(rvm-prompt | awk '{print $1}')"
  fi
}

rb_prompt() {
  if ! [[ -z "$(ruby_version)" ]]
  then
    echo "%{$fg_bold[yellow]%}$(ruby_version)%{$reset_color%}"
  else
    echo ""
  fi
}

directory_name() {
  echo "%{$fg_bold[cyan]%}%1/%\/%{$reset_color%}"
}

running_jobs() {
  local job_count=$(jobs | grep '^\[' | wc -l)
  echo "%{$fg_bold[magenta]%}[$job_count]%{$reset_color%}"
}

cpu_util() {
  local utilization=$(iostat -c | grep -v '^[a-zA-Z]' | grep '[0-9]' | awk '{print int($1+0.5)}')
  local color=blue

  if [ $utilization -gt 60 ]
  then
    color=red
  fi
  echo "%{$fg_bold[$color]%}[$utilization]%{$reset_color%}"
}

prompt_status_bar(){
  local timestamp=$(date +'%r')
  echo "%{$fg_bold[grey]%}$timestamp $(running_jobs)$(cpu_util)%{$reset_color%}"
}

export PROMPT=$'\n$(prompt_status_bar) $(rb_prompt) in $(directory_name) $(git_dirty)$(commit_status)\n› '
set_prompt () {
  export RPROMPT="%{$fg_bold[cyan]%}%{$reset_color%}"
}

precmd() {
  title "zsh" "%m" "%55<...<%~"
  set_prompt
}


Try make git RPROMPT work

Try make git RPROMPT work

# heavily inspired by the wonderful pure theme
# https://github.com/sindresorhus/pure

# needed to get things like current git branch
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git # You can add hg too if needed: `git hg`
zstyle ':vcs_info:git*' use-simple true
zstyle ':vcs_info:git*' max-exports 2
zstyle ':vcs_info:git*' formats ' %b' 'x%R'
zstyle ':vcs_info:git*' actionformats ' %b|%a' 'x%R'

autoload colors && colors

git_dirty() {
    # check if we're in a git repo
    command git rev-parse --is-inside-work-tree &>/dev/null || return

    # check if it's dirty
    command git diff --quiet --ignore-submodules HEAD &>/dev/null;
    if [[ $? -eq 1 ]]; then
        echo "%F{red}✗%f"
    else
        echo "%F{green}✔%f"
    fi
}

# get the status of the current branch and it's remote
# If there are changes upstream, display a ⇣
# If there are changes that have been committed but not yet pushed, display a ⇡
git_arrows() {
    # do nothing if there is no upstream configured
    command git rev-parse --abbrev-ref @'{u}' &>/dev/null || return

    local arrows=""
    local status
    arrow_status="$(command git rev-list --left-right --count HEAD...@'{u}' 2>/dev/null)"

    # do nothing if the command failed
    (( !$? )) || return

    # split on tabs
    arrow_status=(${(ps:\t:)arrow_status})
    local left=${arrow_status[1]} right=${arrow_status[2]}

    (( ${right:-0} > 0 )) && arrows+="%F{011}⇣%f"
    (( ${left:-0} > 0 )) && arrows+="%F{012}⇡%f"

    echo $arrows
}

# indicate a job (for example, vim) has been backgrounded
# If there is a job in the background, display a ✱
suspended_jobs() {
    local sj
    sj=$(jobs 2>/dev/null | tail -n 1)
    if [[ $sj == "" ]]; then
        echo ""
    else
        echo "%{$FG[208]%}✱%f"
    fi
}

precmd() {
    vcs_info
    print -P '\n%F{205}%~'
}

export PROMPT='%(?.%F{205}.%F{red})❯%f '
export RPROMPT='`git_dirty`%F{241}$vcs_info_msg_0_%f `git_arrows``suspended_jobs`'

# heavily inspired by the wonderful pure theme
# https://github.com/sindresorhus/pure

# needed to get things like current git branch
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git # You can add hg too if needed: `git hg`
zstyle ':vcs_info:git*' use-simple true
zstyle ':vcs_info:git*' max-exports 2
zstyle ':vcs_info:git*' formats ' %b' 'x%R'
zstyle ':vcs_info:git*' actionformats ' %b|%a' 'x%R'

autoload colors && colors

git_dirty() {
    # check if we're in a git repo
    command git rev-parse --is-inside-work-tree &>/dev/null || return

    # check if it's dirty
    command git diff --quiet --ignore-submodules HEAD &>/dev/null;
    if [[ $? -eq 1 ]]; then
        echo "%F{red}✗%f"
    else
        echo "%F{green}✔%f"
    fi
}

# get the status of the current branch and it's remote
# If there are changes upstream, display a ⇣
# If there are changes that have been committed but not yet pushed, display a ⇡
git_arrows() {
    # do nothing if there is no upstream configured
    command git rev-parse --abbrev-ref @'{u}' &>/dev/null || return

    local arrows=""
    local status
    arrow_status="$(command git rev-list --left-right --count HEAD...@'{u}' 2>/dev/null)"

    # do nothing if the command failed
    (( !$? )) || return

    # split on tabs
    arrow_status=(${(ps:\t:)arrow_status})
    local left=${arrow_status[1]} right=${arrow_status[2]}

    (( ${right:-0} > 0 )) && arrows+="%F{011}⇣%f"
    (( ${left:-0} > 0 )) && arrows+="%F{012}⇡%f"

    echo $arrows
}

# indicate a job (for example, vim) has been backgrounded
# If there is a job in the background, display a ✱
suspended_jobs() {
    local sj
    sj=$(jobs 2>/dev/null | tail -n 1)
    if [[ $sj == "" ]]; then
        echo ""
    else
        echo "%{$FG[208]%}✱%f"
    fi
}

precmd() {
    vcs_info
    print -P '\n%F{205}%~'
}

export PROMPT='%(?.%F{205}.%F{red})❯%f '
export RPROMPT="`git_dirty`%F{241}$vcs_info_msg_0_%f `git_arrows``suspended_jobs`"


Create junglediskserver-restart-on-fail.sh

First version of script. Just wrote it and haven't test it in production yet, so definitely check it yourself before employing it.

#!/bin/sh

#
# Jungle Disk Server client for Linux has a bug if the Jungle Disk gateway server
# is restarted then the client enters a stuck state where the client never reconnects
# and no backups are run. And there is no warning or error reporting to warn you.
#
# This script is a workaround the checks the age of the backup log file. If it
# has not been updated in X hours they we assume the schedule backups did not run
# and we restart the client.
#
# Unfortunately the command line options were removed from Jungle Disk Server 3.x
# so there is no way to start the missed backups other than the management GUI.
# So I recommend you configure your schedule to run missed backups automatically.
#
# This script is designed to be used in the cron job, and only outputs to stdout/stderr
# if the client needs to be restarted or if the log file is missing.
#
# Aaron Roydhouse <aaron@roydhouse.com>
# Sep 2014
#

# Configure for your environment
#
LOG_FILE=/var/cache/jungledisk/server-cache/logs/backuplog.csv
MAX_AGE_MINS=720
INIT_SCRIPT=/etc/init.d/junglediskserver

# Check for log file
#
if [ ! -r $LOG_FILE ]
then
  echo "Unable to read Jungle Disk Server log file: $LOG_FILE"
fi

# If the log file is older than expected, restart the client
#
if test `find $LOG_FILE -mmin +$MAX_AGE_MINS`
then
  echo "Jungle Disk Server appears stuck, restarting"
  echo "At `date` `basename $LOG_FILE` is more than $MAX_AGE_MINS minutes old"
  $INIT_SCRIPT restart
fi

# end

error: pathspec 'junglediskserver-restart-on-fail.sh' did not match any file(s) known to git


add gen_pki.sh

add gen_pki.sh

#/bin/bash

# Copyright 2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

# This script generates the crypto necessary for the SSL tests.

DIR=`dirname "$0"`
CURRENT_DIR=`cd "$DIR" && pwd`
CERTS_DIR=$CURRENT_DIR/certs
PRIVATE_DIR=$CURRENT_DIR/private
CMS_DIR=$CURRENT_DIR/cms

function rm_old {
    rm -rf $CERTS_DIR/*.pem
    rm -rf $PRIVATE_DIR/*.pem
}

function cleanup {
    rm -rf *.conf > /dev/null 2>&1
    rm -rf index* > /dev/null 2>&1
    rm -rf *.crt > /dev/null 2>&1
    rm -rf newcerts > /dev/null 2>&1
    rm -rf *.pem > /dev/null 2>&1
    rm -rf serial* > /dev/null 2>&1
}

function generate_ca_conf {
    echo '
[ req ]
default_bits            = 2048
default_keyfile         = cakey.pem
default_md              = sha256

prompt                  = no
distinguished_name      = ca_distinguished_name

x509_extensions         = ca_extensions

[ ca_distinguished_name ]
serialNumber            = 5
countryName             = TW
stateOrProvinceName     = KH
localityName            = Kaohsiung
organizationName        = InfinitiesSoft
organizationalUnitName  = Keystone4j
emailAddress            = pohsun@infinitiessoft.com
commonName              = Self Signed

[ ca_extensions ]
basicConstraints        = critical,CA:true
' > ca.conf
}

function generate_ssl_req_conf {
    echo '
[ req ]
default_bits            = 2048
default_keyfile         = keystonekey.pem
default_md              = sha256

prompt                  = no
distinguished_name      = distinguished_name

[ distinguished_name ]
countryName             = TW
stateOrProvinceName     = KH
localityName            = Kaohsiung
organizationName        = InfinitiesSoft
organizationalUnitName  = Keystone4j
commonName              = localhost
emailAddress            = pohsun@infinitiessoft.com
' > ssl_req.conf
}

function generate_cms_signing_req_conf {
    echo '
[ req ]
default_bits            = 2048
default_keyfile         = keystonekey.pem
default_md              = sha256

prompt                  = no
distinguished_name      = distinguished_name

[ distinguished_name ]
countryName             = TW
stateOrProvinceName     = KH
localityName            = Kaohsiung
organizationName        = InfinitiesSoft
organizationalUnitName  = Keystone4j
commonName              = Keystone4j
emailAddress            = pohsun@infinitiessoft.com
' > cms_signing_req.conf
}

function generate_signing_conf {
    echo '
[ ca ]
default_ca      = signing_ca

[ signing_ca ]
dir             = .
database        = $dir/index.txt
new_certs_dir   = $dir/newcerts

certificate     = $dir/certs/ca.pem
serial          = $dir/serial
private_key     = $dir/private/cakey.pem

default_days            = 21360
default_crl_days        = 30
default_md              = sha256

policy                  = policy_any

[ policy_any ]
countryName             = supplied
stateOrProvinceName     = supplied
localityName            = optional
organizationName        = supplied
organizationalUnitName  = supplied
emailAddress            = supplied
commonName              = supplied
' > signing.conf
}

function setup {
    touch index.txt
    echo '10' > serial
    generate_ca_conf
    mkdir newcerts
}

function check_error {
    if [ $1 != 0 ] ; then
        echo "Failed! rc=${1}"
        echo 'Bailing ...'
        cleanup
        exit $1
    else
        echo 'Done'
    fi
}

function generate_ca {
    echo 'Generating New CA Certificate ...'
    openssl req -x509 -newkey rsa:2048 -days 21360 -out $CERTS_DIR/ca.pem \
        -keyout $PRIVATE_DIR/cakey.pem -outform PEM -config ca.conf -nodes
    check_error $?
}

function ssl_cert_req {
    echo 'Generating SSL Certificate Request ...'
    generate_ssl_req_conf
    openssl req -newkey rsa:2048 -keyout $PRIVATE_DIR/ssl_key.pem \
        -keyform PEM -out ssl_req.pem -outform PEM -config ssl_req.conf -nodes
    check_error $?
    #openssl req -in req.pem -text -noout
}

function cms_signing_cert_req {
    echo 'Generating CMS Signing Certificate Request ...'
    generate_cms_signing_req_conf
    openssl req -newkey rsa:2048 -keyout $PRIVATE_DIR/signing_key.pem \
        -keyform PEM -out cms_signing_req.pem -outform PEM \
        -config cms_signing_req.conf -nodes
    check_error $?
    #openssl req -in req.pem -text -noout
}

function issue_certs {
    generate_signing_conf
    echo 'Issuing SSL Certificate ...'
    openssl ca -in ssl_req.pem -config signing.conf -batch
    check_error $?
    openssl x509 -in $CURRENT_DIR/newcerts/10.pem -out $CERTS_DIR/ssl_cert.pem
    check_error $?
    echo 'Issuing CMS Signing Certificate ...'
    openssl ca -in cms_signing_req.pem -config signing.conf -batch
    check_error $?
    openssl x509 -in $CURRENT_DIR/newcerts/11.pem \
        -out $CERTS_DIR/signing_cert.pem
    check_error $?
}

function create_middleware_cert {
    cp $CERTS_DIR/ssl_cert.pem $CERTS_DIR/middleware.pem
    cat $PRIVATE_DIR/ssl_key.pem >> $CERTS_DIR/middleware.pem
}

function check_openssl {
    echo 'Checking openssl availability ...'
    which openssl
    check_error $?
}

function gen_sample_cms {
    FILES="${CMS_DIR}/auth_token_revoked.json"
    FILES+=" ${CMS_DIR}/auth_token_unscoped.json"
    FILES+=" ${CMS_DIR}/auth_token_scoped.json"
    FILES+=" ${CMS_DIR}/revocation_list.json"
    for json_file in $FILES; do
        openssl cms -sign -in $json_file -nosmimecap \
            -signer $CERTS_DIR/signing_cert.pem \
            -inkey $PRIVATE_DIR/signing_key.pem -outform PEM -nodetach \
            -nocerts -noattr -out ${json_file/.json/.pem}
    done
}

function create_p12 {
    echo 'Issuing keystone.p12 Certificate ...'
    openssl pkcs12 -export -password pass:changeit -in $CERTS_DIR/ssl_cert.pem -inkey $PRIVATE_DIR/ssl_key.pem -out keystone4j.p12 -name "keystone4j"
    check_error $?
}

check_openssl
rm_old
cleanup
setup
generate_ca
ssl_cert_req
cms_signing_cert_req
issue_certs
create_middleware_cert
create_p12
#gen_sample_cms
cleanup

error: pathspec 'keystone4j-core/conf/ssl/gen_pki.sh' did not match any file(s) known to git


Create S-Type_Installer.sh

Create S-Type_Installer.sh

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/torksrevol/torksrevol.github.io.git/'


Add test for version-origin when version not found in `rbenv-exec`

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/yyyc514/rbenv.git/'


Uncomment the dso for apache for passenger - waste of RAM if you're not using passenger to manage rails apps.

#!/opt/local/bin/bash
# Disables the passenger plugin on Joyent SmartMachines
#
# Copyright (c) 2011-2012 Jacques Marneweck <jacques@powertrip.co.za>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

gsed -i'' '/passenger.conf/s/^/#/g' /opt/local/etc/httpd/httpd.conf
gsed -i'' '/passenger.so/s/^/#/g' /opt/local/etc/httpd/includes/dso.conf

svcadm restart apache

error: pathspec 'disable-passenger.sh' did not match any file(s) known to git