Skip to content
Snippets Groups Projects
freedombone-app-ipfs 11.2 KiB
Newer Older
Bob Mottram's avatar
Bob Mottram committed
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
Bob Mottram's avatar
Bob Mottram committed
#
# ipfs functions
#
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
Bob Mottram's avatar
Bob Mottram committed
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

Bob Mottram's avatar
Bob Mottram committed
VARIANTS=''
NOT_ON_HOMEPAGE=1
Bob Mottram's avatar
Bob Mottram committed
IPFS_GO_REPO_BASE="github.com/ipfs/go-ipfs"
Bob Mottram's avatar
Bob Mottram committed
IPFS_GO_REPO="https://$IPFS_GO_REPO_BASE"
IPFS_COMMIT='6fdfaaf6e4783ae1be7b348e7a6bc0640982c7df'
Bob Mottram's avatar
Bob Mottram committed
IPFS_PORT=4001
IPFS_NODE_VERSION='6.2.2'
Bob Mottram's avatar
Bob Mottram committed
IPFS_N_VERSION='2.1.4'
Bob Mottram's avatar
Bob Mottram committed
IPFS_KEY_LENGTH=2048
Bob Mottram's avatar
Bob Mottram committed
IPFS_GO_VERSION=0.4.13
Bob Mottram's avatar
Bob Mottram committed

ipfs_variables=(IPFS_GO_VERSION
Bob Mottram's avatar
Bob Mottram committed
                IPFS_KEY_LENGTH
                IPFS_PORT)

function logging_on_ipfs {
    echo -n ''
}

function logging_off_ipfs {
    echo -n ''
}

function install_interactive_ipfs {
    echo -n ''
    APP_INSTALLED=1
function reconfigure_ipfs {
function upgrade_ipfs_go {
Bob Mottram's avatar
Bob Mottram committed
    CURR_IPFS_COMMIT=$(get_completion_param "ipfs commit")
    if [[ "$CURR_IPFS_COMMIT" == "$IPFS_COMMIT" ]]; then
        return
    fi

Bob Mottram's avatar
Bob Mottram committed
    if [[ $(app_is_installed ipfs_go) == "0" ]]; then
        return
    fi

    function_check set_repo_commit
Bob Mottram's avatar
Bob Mottram committed
    set_repo_commit "$GOPATH/src/github.com/ipfs/go-ipfs" "ipfs commit" "$IPFS_COMMIT" "$IPFS_REPO"
function upgrade_ipfs {
    upgrade_ipfs_go
Bob Mottram's avatar
Bob Mottram committed
function backup_local_ipfs {
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "Admin user" "$COMPLETION_FILE"; then
Bob Mottram's avatar
Bob Mottram committed
    ADMIN_USERNAME=$(get_completion_param "Admin user")
Bob Mottram's avatar
Bob Mottram committed
    source_directory="/home/$ADMIN_USERNAME/.ipfs"
    if [ -d "$source_directory" ]; then
        dest_directory=ipfs
        function_check backup_directory_to_usb
Bob Mottram's avatar
Bob Mottram committed
        backup_directory_to_usb "$source_directory" "$dest_directory"
function restore_local_ipfs {
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "Admin user" "$COMPLETION_FILE"; then
Bob Mottram's avatar
Bob Mottram committed
    if [ -d "$USB_MOUNT/backup/ipfs" ]; then
        echo $"Restoring IPFS"
        temp_restore_dir=/root/tempipfs
        function_check restore_directory_from_usb
        restore_directory_from_usb $temp_restore_dir ipfs
Bob Mottram's avatar
Bob Mottram committed
        ADMIN_USERNAME=$(get_completion_param "Admin user")
Bob Mottram's avatar
Bob Mottram committed
        if [ -d "$temp_restore_dir/home/$ADMIN_USERNAME/.ipfs" ]; then
Bob Mottram's avatar
Bob Mottram committed
            cp -rf "$temp_restore_dir/home/$ADMIN_USERNAME/.ipfs/"* "/home/$ADMIN_USERNAME/.ipfs"
Bob Mottram's avatar
Bob Mottram committed
            cp -rf "$temp_restore_dir/"* "/home/$ADMIN_USERNAME/.ipfs"
Bob Mottram's avatar
Bob Mottram committed
        # shellcheck disable=SC2181
        if [ ! "$?" = "0" ]; then
            rm -rf $temp_restore_dir
            function_check set_user_permissions
            set_user_permissions
            function_check backup_unmount_drive
            backup_unmount_drive
            exit 27627
        fi
        rm -rf $temp_restore_dir
        echo $"Restore of IPFS complete"
    fi
Bob Mottram's avatar
Bob Mottram committed
function backup_remote_ipfs {
    echo -n ''
}

function restore_remote_ipfs {
    echo -n ''
}

function remove_ipfs_go {
    systemctl stop ipfs
    systemctl disable ipfs
    systemctl daemon-reload
    rm /etc/systemd/system/ipfs.service
Bob Mottram's avatar
Bob Mottram committed
    systemctl daemon-reload
Bob Mottram's avatar
Bob Mottram committed
    rm -rf "$GOPATH/src/github.com/ipfs"
Bob Mottram's avatar
Bob Mottram committed
    firewall_remove $IPFS_PORT tcp
Bob Mottram's avatar
Bob Mottram committed
    remove_completion_param install_ipfs
    remove_completion_param configure_firewall_for_ipfs
Bob Mottram's avatar
Bob Mottram committed
    sed -i '/ipfs/d' "$COMPLETION_FILE"
Bob Mottram's avatar
Bob Mottram committed
function remove_ipfs {
    remove_ipfs_go
function configure_firewall_for_ipfs {
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
        return
    fi
    if [[ $ONION_ONLY != "no" ]]; then
        return
    fi

Bob Mottram's avatar
Bob Mottram committed
    firewall_add IPFS $IPFS_PORT tcp
Bob Mottram's avatar
Bob Mottram committed
    mark_completed "${FUNCNAME[0]}"
function mesh_install_ipfs_go {
    # shellcheck disable=SC2086
    chroot "${rootdir}" $INSTALL_PACKAGES libpam0g-dev fuse wget
Bob Mottram's avatar
Bob Mottram committed
    IPFS_ARCH=
    IPFS_PATH=/usr/bin
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "$rootdir$INSTALL_DIR/ipfs" ]; then
        mkdir -p "$rootdir$INSTALL_DIR/ipfs"
Bob Mottram's avatar
Bob Mottram committed
    fi
Bob Mottram's avatar
Bob Mottram committed
    cd "$rootdir$INSTALL_DIR/ipfs" || exit 23468264
Bob Mottram's avatar
Bob Mottram committed

Bob Mottram's avatar
Bob Mottram committed
    if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        IPFS_ARCH=386
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [[ $ARCHITECTURE == *"amd64" || $ARCHITECTURE == "x86_64" ]]; then
Bob Mottram's avatar
Bob Mottram committed
        IPFS_ARCH=amd64
    fi
    if [[ $ARCHITECTURE == *"arm"* || $ARCHITECTURE == *"aarch"* ]]; then
Bob Mottram's avatar
Bob Mottram committed
        IPFS_ARCH=arm
    fi
    if [ ! $IPFS_ARCH ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $'No architecture specified'
        ARCHITECTURE=$(uname -m)
        if [[ $ARCHITECTURE == "arm"* || $ARCHITECTURE == *"aarch"* ]]; then
Bob Mottram's avatar
Bob Mottram committed
            IPFS_ARCH=arm
            echo $"Using $IPFS_ARCH"
Bob Mottram's avatar
Bob Mottram committed
        fi
        if [[ $ARCHITECTURE == "amd"* || $ARCHITECTURE == "x86_64" ]]; then
            IPFS_ARCH=amd64
            echo $"Using $IPFS_ARCH"
Bob Mottram's avatar
Bob Mottram committed
        fi
        if [[ $ARCHITECTURE == *"386" || $ARCHITECTURE == *"686" ]]; then
            IPFS_ARCH=386
            echo $"Using $IPFS_ARCH"
Bob Mottram's avatar
Bob Mottram committed
        fi
Bob Mottram's avatar
Bob Mottram committed
    fi
    if [ ! $IPFS_ARCH ]; then
        echo $'Architecture not detected for IPFS install'
        exit 738765235
    fi
Bob Mottram's avatar
Bob Mottram committed
    IPFS_FILE=go-ipfs_v${IPFS_GO_VERSION}_linux-${IPFS_ARCH}.tar.gz
    wget https://ipfs.io/ipns/dist.ipfs.io/go-ipfs/v${IPFS_GO_VERSION}/${IPFS_FILE}
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$rootdir$INSTALL_DIR/ipfs/${IPFS_FILE}" ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $'IPFS package could not be downloaded'
        exit 63725
    fi
    tar -xzvf ${IPFS_FILE}
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$rootdir$INSTALL_DIR/ipfs/go-ipfs/ipfs" ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $"ipfs was not found in downloaded package"
        exit 638235
    fi
Bob Mottram's avatar
Bob Mottram committed
    chroot "$rootdir" /bin/cp "$INSTALL_DIR/ipfs/go-ipfs/ipfs" $IPFS_PATH
    if [ ! -f "$rootdir$IPFS_PATH/ipfs" ]; then
Bob Mottram's avatar
Bob Mottram committed
        echo $'IPFS was not installed'
        exit 63722
Bob Mottram's avatar
Bob Mottram committed
    IPFS_USER_DAEMON=${rootdir}/home/${MY_USERNAME}/.config/systemd/user/ipfs.service
Bob Mottram's avatar
Bob Mottram committed
    mkdir -p "${rootdir}/home/${MY_USERNAME}/.config/systemd/user"

    { echo '[Unit]';
      echo 'Description=IPFS go daemon';
      echo 'After=syslog.target';
      echo 'After=network.target';
      echo '';
      echo '[Service]';
      echo 'Type=simple';
      echo "ExecStart=$IPFS_PATH/ipfs daemon";
      echo 'Restart=on-failure';
      echo 'RestartSec=60';
      echo '';
      echo '[Install]';
      echo 'WantedBy=default.target'; } > "$IPFS_USER_DAEMON"
    chroot "${rootdir}" /bin/chown -R "$MY_USERNAME":"$MY_USERNAME" "/home/${MY_USERNAME}/.config"

function install_ipfs_go {
Bob Mottram's avatar
Bob Mottram committed
    if [ "$INSTALLING_MESH" ]; then
        mesh_install_ipfs_go
        return
    fi
Bob Mottram's avatar
Bob Mottram committed
    if [[ $(app_is_installed ipfs_go) == "1" ]]; then
    $INSTALL_PACKAGES golang libpam0g-dev fuse

    if [ ! -d /home/git ]; then
        # add a gogs user account
        adduser --disabled-login --gecos 'Gogs' git
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -d /home/git ]; then
            echo $"/home/git directory not created"
            exit 735272
        fi

        # install Go
        if ! grep -q "export GOPATH=" ~/.bashrc; then
            echo "export GOPATH=$GOPATH" >> ~/.bashrc
        else
            sed -i "s|export GOPATH=.*|export GOPATH=$GOPATH|g" ~/.bashrc
        fi
Bob Mottram's avatar
Bob Mottram committed
        systemctl set-environment GOPATH="$GOPATH"
        if ! grep -q "systemctl set-environment GOPATH=" ~/.bashrc; then
            echo "systemctl set-environment GOPATH=$GOPATH" >> ~/.bashrc
        else
            sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=$GOPATH|g" ~/.bashrc
        fi
Bob Mottram's avatar
Bob Mottram committed
        if [ ! -d "$GOPATH" ]; then
            mkdir -p "$GOPATH"
        fi
    fi

    IPFS_PATH=$GOPATH/bin
    export PATH="$GOPATH/bin:$PATH:"
    if ! grep -q 'GOPATH/bin' ~/.bashrc; then
Bob Mottram's avatar
Bob Mottram committed
        echo "export PATH=\"\$GOPATH/bin:\$PATH:\";" >> ~/.bashrc
    else
        sed -i "s|systemctl set-environment GOPATH=.*|systemctl set-environment GOPATH=$GOPATH|g" ~/.bashrc
    fi

    # set gopath for the user
Bob Mottram's avatar
Bob Mottram committed
    if ! grep -q "GOPATH=" "/home/$MY_USERNAME/.bashrc"; then
        echo "export GOPATH=$GOPATH" >> "/home/$MY_USERNAME/.bashrc"
        echo "export PATH=\"\$GOPATH/bin:\$PATH:\";" >> "/home/$MY_USERNAME/.bashrc"
Bob Mottram's avatar
Bob Mottram committed
        sed -i "s|export GOPATH=.*|export GOPATH=$GOPATH|g" "/home/$MY_USERNAME/.bashrc"
Bob Mottram's avatar
Bob Mottram committed
    chown "$MY_USERNAME":"$MY_USERNAME" "/home/$MY_USERNAME/.bashrc"
Bob Mottram's avatar
Bob Mottram committed
    if ! go get -u ${IPFS_GO_REPO_BASE}/cmd/ipfs; then
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d "$GOPATH/src/$IPFS_GO_REPO_BASE" ]; then
        echo $'go get failed to get ipfs'
        exit 63923
    fi

Bob Mottram's avatar
Bob Mottram committed
    cd "$GOPATH/src/$IPFS_GO_REPO_BASE" || exit 346824
    if ! git checkout $IPFS_COMMIT -b $IPFS_COMMIT; then
Bob Mottram's avatar
Bob Mottram committed
        exit 735639
    fi

Bob Mottram's avatar
Bob Mottram committed
    set_completion_param "ipfs commit" "$IPFS_COMMIT"
Bob Mottram's avatar
Bob Mottram committed
    if ! make install; then
Bob Mottram's avatar
Bob Mottram committed
    chmod 755 "$IPFS_PATH/ipfs"
    su -c "$IPFS_PATH/ipfs init -b 2048" - "$MY_USERNAME"
    if [ ! -d "/home/$MY_USERNAME/.ipfs" ]; then
        echo "IPFS could not be initialised for user $MY_USERNAME"
        exit 7358
    fi

    # directories to mount to
    if [ ! -d /ipfs ]; then
        mkdir /ipfs
        mkdir /ipns
Bob Mottram's avatar
Bob Mottram committed
        chown "$MY_USERNAME":"$MY_USERNAME" /ipfs
        chown "$MY_USERNAME":"$MY_USERNAME" /ipns
    fi

    if [ -f /etc/fuse.conf ]; then
Bob Mottram's avatar
Bob Mottram committed
        chown "$MY_USERNAME":"$MY_USERNAME" /etc/fuse.conf
    fi
    if [ -f /dev/fuse ]; then
Bob Mottram's avatar
Bob Mottram committed
        chown "$MY_USERNAME":"$MY_USERNAME" /dev/fuse
    fi

    { echo '[Unit]';
      echo 'Description=IPFS go daemon';
      echo 'After=syslog.target';
      echo 'After=network.target';
      echo '';
      echo '[Service]';
      echo 'Type=simple';
      echo "User=$MY_USERNAME";
      echo "Group=$MY_USERNAME";
      echo "WorkingDirectory=/home/$MY_USERNAME";
      echo "ExecStart=$IPFS_PATH/ipfs daemon --mount";
      echo 'Restart=on-failure';
      echo "Environment=\"USER=$MY_USERNAME\" \"HOME=/home/$MY_USERNAME\" \"GOPATH=$GOPATH\" \"GVM_ROOT=$GVM_HOME\"";
      echo '';
      echo '[Install]';
      echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/ipfs.service

    systemctl enable ipfs
    systemctl daemon-reload
    systemctl restart ipfs

    if [ -d /etc/avahi ]; then
Bob Mottram's avatar
Bob Mottram committed
        su -c "echo $("$IPFS_PATH/ipfs" id | grep '\"ID\":' | awk -F '\"' '{print $4}') > /tmp/ipfsid" - "$MY_USERNAME"
        if [ ! -f /tmp/ipfsid ]; then
            echo 'No IPFS identity was created'
            exit 37895
        fi
        IPFS_PEER_ID=$(cat /tmp/ipfsid)
        if [ ${#IPFS_PEER_ID} -lt 10 ]; then
            echo 'Invalid IPFS peer ID'
            echo "$IPFS_PEER_ID"
            exit 74782
        fi
        # Add an avahi service
        function_check create_avahi_service
Bob Mottram's avatar
Bob Mottram committed
        create_avahi_service ipfs_id "ipfs_id" 'udp' $IPFS_PORT "$IPFS_PEER_ID:none"
        rm /tmp/ipfsid
    fi

    function_check configure_firewall_for_ipfs
    configure_firewall_for_ipfs

Bob Mottram's avatar
Bob Mottram committed
    install_completed ipfs_go
Bob Mottram's avatar
Bob Mottram committed
function install_ipfs {
    install_ipfs_go
Bob Mottram's avatar
Bob Mottram committed
}