<commit_before>#!/bin/sh
# Temporary wrapper for OVS until native Docker integration is available upstream
set -e

usage() {
    cat << EOF
NAME:
    socketplane - Install, Configure and Run SocketPlane

VERSION:
    0.1

USAGE:
    $0 <options> <command> [command_options] [arguments...]

OPTIONS:

    -D      Debug

COMMANDS:
    help
            Help and usage

    install [unattended]
            Install SocketPlane (installs docker and openvswitch)

    uninstall
            Remove Socketplane installation

    clean
            Remove Socketplane installation and dependencies (docker and openvswitch)

    deps
            Show SocketPlane dependencies

    agent {stop|start|logs}
            Start/Stop the SocketPlane container or show its logs

    info [container_id]
            Show SocketPlane info for all containers, or for a given container_id

    run [-n foo] <docker_run_args>
            Run a container and optionally specify which network to attach to

    start <container_id>
            Start a <container_id>

    stop <container_id>
            Stop the <container_id>

    rm <container_id>
            Remove the <container_id>

    attach <container_id>
            Attach to the <container_id>

    network list
            List all created networks

    network info <name>
            Display information about a given network

    network create <name> [cidr]
            Create a network

    network delete <name> [cidr]
            Delete a network

    network agent start
            Starts an existing SocketPlane image if it is not already running

    network agent stop
            Stops a running SocketPlane image. This will not delete the local image

EOF
}

basedir=$(dirname $(readlink -m $0))
. $basedir/functions.sh

# Utility function to attach a port to a network namespace
attach()    # OVS Port ID
            # IP Address
            # Subnet
            # MAC Address
            # Gateway IP
            # Namespace PID
{
    # see: https://docs.docker.com/articles/networking/

    [ ! -d /var/run/netns ] && mkdir -p /var/run/netns
    [ -f /var/run/netns/$6 ] && rm -f /var/run/netns/$6
    ln -s /proc/$6/ns/net /var/run/netns/$6

    ip link set dev $1 netns $6
    ip netns exec $6 ip link set dev $1 address $4
    ip netns exec $6 ip link set dev $1 up
    ip netns exec $6 ip addr add $2$3 dev $1
    ip netns exec $6 ip route add default via $5

}

get_status() {
    OS="NOT_LINUX"
    RELEASE="NOT_LINUX"
    CODENAME="NOT_LINUX"
    ARCH=$(uname -m)

    if [ "$ARCH" = "x86_64" ]; then
        ARCH="amd64";
    fi

    if [ "$ARCH" = "i686" ]; then
        ARCH="i386";
    fi

    if command_exists lsb_release; then
        OS=$(lsb_release -is)
        RELEASE=$(lsb_release -rs)
        CODENAME=$(lsb_release -cs)
    elif [ -f /etc/debian_version ]; then
        OS="Debian"
        RELEASE="UNDETECTED"
        CODENAME="UNDETECTED"
    elif [ -f /etc/redhat-release ]; then
        OS="RedHat"
        RELEASE="UNDETECTED"
        CODENAME="UNDETECTED"
    fi

    DOCKER_SVER="NOT_INSTALLED"
    DOCKER_CVER="NOT_INSTALLED"
    if command_exists docker || command_exists lxc-docker; then
        DOCKER_SVER=$(docker version | grep "Server version:" |  awk '{ print $3 }')
        DOCKER_CVER=$(docker version | grep "Client API version:" |  awk '{ print $4 }')
    fi

    OVS_SVER="NOT_INSTALLED"
    if command_exists ovsdb-server && command_exists ovs-vswitchd ; then
        OVS_SVER=$(ovs-appctl -V | grep "ovs-" |  awk '{ print $4 }')
    fi
}

deps() {
    echo "Socketplane  Docker Host Requirements:"
    echo ".. Open vSwitch Environment:"
    echo ".... Archicture:              amd64 or i386"
    echo "....   Current:               $ARCH"
    echo ".... Operating System:         Ubuntu, Debian and Fedora"
    echo "....   Current:               $lsb_dist"
    echo ".... Open vSwitch Version:     2.1 or higher"
    echo "....   Current:               $OVS_SVER"
    echo ".. Docker Environment:"
    echo ".... Docker Server Version:   1.4 or higher"
    echo "....   Current:               $DOCKER_SVER"
    echo ".... Docker Client Version:   1.16 or higher"
    echo "....   Current:               $DOCKER_SVER"
}

kernel_opts(){
    log_step "Setting Linux Kernel Options"
    if [ $(cat /etc/sysctl.conf | grep icmp_echo_ignore_broadcasts) ]; then
        sed -i 's/^#\?net\.ipv4\.icmp_echo_ignore_broadcasts.*$/net\.ipv4\.icmp_echo_ignore_broadcasts=0/g' /etc/sysctl.conf
    else
        echo 'net.ipv4.icmp_echo_ignore_broadcasts=0' >> /etc/sysctl.conf
    fi

    if [ $(cat /etc/sysctl.conf | grep ip_forward ) ]; then
        sed -i 's/^#\?net\.ipv4\.ip_forward.*$/net\.ipv4\.ip_forward=1/g' /etc/sysctl.conf
    else
        echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
    fi
    sysctl -p | indent
}

pkg_update() {
    log_step "Ensuring Package Repositories are up to date"
    $pkg update > /dev/null
}

install_curl() {
    if command_exists curl; then
        log_step "Curl already installed!"
    else
        log_step "Installing Curl"
        $pkg install curl | indent
    fi
}

install_ovs() {
    if command_exists ovsdb-server && command_exists ovs-vswitchd ; then
        log_step "Open vSwitch already installed!"
    else
        log_step "Installing Open vSwitch.."
        $pkg install $ovs | indent
    fi

    # Make sure the processes are started<commit_msg>bug#60 Fedora SP image fails to start due to SELINUX<commit_after>#!/bin/sh
# Temporary wrapper for OVS until native Docker integration is available upstream
set -e

usage() {
    cat << EOF
NAME:
    socketplane - Install, Configure and Run SocketPlane

VERSION:
    0.1

USAGE:
    $0 <options> <command> [command_options] [arguments...]

OPTIONS:

    -D      Debug

COMMANDS:
    help
            Help and usage

    install [unattended]
            Install SocketPlane (installs docker and openvswitch)

    uninstall
            Remove Socketplane installation

    clean
            Remove Socketplane installation and dependencies (docker and openvswitch)

    deps
            Show SocketPlane dependencies

    agent {stop|start|logs}
            Start/Stop the SocketPlane container or show its logs

    info [container_id]
            Show SocketPlane info for all containers, or for a given container_id

    run [-n foo] <docker_run_args>
            Run a container and optionally specify which network to attach to

    start <container_id>
            Start a <container_id>

    stop <container_id>
            Stop the <container_id>

    rm <container_id>
            Remove the <container_id>

    attach <container_id>
            Attach to the <container_id>

    network list
            List all created networks

    network info <name>
            Display information about a given network

    network create <name> [cidr]
            Create a network

    network delete <name> [cidr]
            Delete a network

    network agent start
            Starts an existing SocketPlane image if it is not already running

    network agent stop
            Stops a running SocketPlane image. This will not delete the local image

EOF
}

basedir=$(dirname $(readlink -m $0))
. $basedir/functions.sh

# Utility function to attach a port to a network namespace
attach()    # OVS Port ID
            # IP Address
            # Subnet
            # MAC Address
            # Gateway IP
            # Namespace PID
{
    # see: https://docs.docker.com/articles/networking/

    [ ! -d /var/run/netns ] && mkdir -p /var/run/netns
    [ -f /var/run/netns/$6 ] && rm -f /var/run/netns/$6
    ln -s /proc/$6/ns/net /var/run/netns/$6

    ip link set dev $1 netns $6
    ip netns exec $6 ip link set dev $1 address $4
    ip netns exec $6 ip link set dev $1 up
    ip netns exec $6 ip addr add $2$3 dev $1
    ip netns exec $6 ip route add default via $5

}

get_status() {
    OS="NOT_LINUX"
    RELEASE="NOT_LINUX"
    CODENAME="NOT_LINUX"
    ARCH=$(uname -m)

    if [ "$ARCH" = "x86_64" ]; then
        ARCH="amd64";
    fi

    if [ "$ARCH" = "i686" ]; then
        ARCH="i386";
    fi

    if command_exists lsb_release; then
        OS=$(lsb_release -is)
        RELEASE=$(lsb_release -rs)
        CODENAME=$(lsb_release -cs)
    elif [ -f /etc/debian_version ]; then
        OS="Debian"
        RELEASE="UNDETECTED"
        CODENAME="UNDETECTED"
    elif [ -f /etc/redhat-release ]; then
        OS="RedHat"
        RELEASE="UNDETECTED"
        CODENAME="UNDETECTED"
    fi

    DOCKER_SVER="NOT_INSTALLED"
    DOCKER_CVER="NOT_INSTALLED"
    if command_exists docker || command_exists lxc-docker; then
        DOCKER_SVER=$(docker version | grep "Server version:" |  awk '{ print $3 }')
        DOCKER_CVER=$(docker version | grep "Client API version:" |  awk '{ print $4 }')
    fi

    OVS_SVER="NOT_INSTALLED"
    if command_exists ovsdb-server && command_exists ovs-vswitchd ; then
        OVS_SVER=$(ovs-appctl -V | grep "ovs-" |  awk '{ print $4 }')
    fi
}

deps() {
    echo "Socketplane  Docker Host Requirements:"
    echo ".. Open vSwitch Environment:"
    echo ".... Archicture:              amd64 or i386"
    echo "....   Current:               $ARCH"
    echo ".... Operating System:         Ubuntu, Debian and Fedora"
    echo "....   Current:               $lsb_dist"
    echo ".... Open vSwitch Version:     2.1 or higher"
    echo "....   Current:               $OVS_SVER"
    echo ".. Docker Environment:"
    echo ".... Docker Server Version:   1.4 or higher"
    echo "....   Current:               $DOCKER_SVER"
    echo ".... Docker Client Version:   1.16 or higher"
    echo "....   Current:               $DOCKER_SVER"
}

kernel_opts(){
    log_step "Setting Linux Kernel Options"
    if [ $(cat /etc/sysctl.conf | grep icmp_echo_ignore_broadcasts) ]; then
        sed -i 's/^#\?net\.ipv4\.icmp_echo_ignore_broadcasts.*$/net\.ipv4\.icmp_echo_ignore_broadcasts=0/g' /etc/sysctl.conf
    else
        echo 'net.ipv4.icmp_echo_ignore_broadcasts=0' >> /etc/sysctl.conf
    fi

    if [ $(cat /etc/sysctl.conf | grep ip_forward ) ]; then
        sed -i 's/^#\?net\.ipv4\.ip_forward.*$/net\.ipv4\.ip_forward=1/g' /etc/sysctl.conf
    else
        echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
    fi
    sysctl -p | indent
}

pkg_update() {
    log_step "Ensuring Package Repositories are up to date"
    $pkg update > /dev/null
}

install_curl() {
    if command_exists curl; then
        log_step "Curl already installed!"
    else
        log_step "Installing Curl"
        $pkg install curl | indent
    fi
}

install_ovs() {
    if command_exists ovsdb-server && command_exists ovs-vswitchd ; then
        log_step "Open vSwitch already installed!"
    else
        if ! command getenforce  2>/dev/null || [[ $(getenforce) =~ Enforcing|Permissive ]] ; then
        log_step "Checking Open vSwitch dependencies.."
        $pkg install policycoreutils-python
        sudo semodule -d openvswitch  2>/dev/null || true
        fi
        log_step "Installing Open vSwitch.."
        $pkg install $ovs | indent
    fi

    # Make sure the processes are started
{'fraction_non_alphanumeric': 0.1029610296102961, 'fraction_numerical': 0.01080010800108001, 'mean_word_length': 2.134555712270804, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7424069', 'n_tokens_mistral': 3696, 'n_tokens_neox': 3487, 'n_words': 1064}