Skip to content

Commit

Permalink
Merge upstart fixes after more testing on RPi and Orange Pi (#7)
Browse files Browse the repository at this point in the history
* Fixes for upstart (#6)
* Send uninstall outputs to /dev/null
  • Loading branch information
claudiobizzotto authored Dec 19, 2017
1 parent d8a0824 commit d77046e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 51 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ A bare bones [IPFS](https://ipfs.io) installer for the Raspberry Pi and other AR

## Installation

Log into your Raspberry Pi with an administrator account (you can use the default `pi` user for this). From any local
directory, clone or download this repo, `cd` into it and run the installer:
Log into your system with an administrator account, like the default OS user. For example, on the Raspberry Pi, most
operating systems will default to the `pi` user, whereas on the Orange Pi that's going to be `orangepi`.

From any local directory, clone or download this repo, `cd` into it and run the installer:

```SHELL
$ ./install
Expand All @@ -15,8 +17,8 @@ $ ./install
**Notes**

* Do **not** execute the installation script with `sudo`
* You'll need root privileges to run the installer (the `pi` user does so by default)
* The IPFS user directory will be created at `~/.ipfs` (eg.: `/home/pi/.ipfs`)
* You'll need root privileges to run the installer. The default OS user (`pi`, `orangepi` etc.) does so by default
* The IPFS user directory will be created at `~/.ipfs` (eg.: `/home/pi/.ipfs`, `/home/orangepi/.ipfs` etc.)

### Options

Expand All @@ -42,12 +44,13 @@ $ ./uninstall
- [x] RPi 1
- [x] RPi 2
- [x] RPi 3
- [ ] Orange Pi
- [x] Orange Pi

**Operating system**:

- [ ] Noobs
- [x] Raspbian
- [x] Ubuntu 14.04 (Trusty)

## How to contribute

Expand Down
55 changes: 25 additions & 30 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

set -o pipefail

#
source ./lib/functions.sh

[ -x "$(command -v ipfs)" ] && echo ">>> IPFS already installed. Use the uninstall script to remove it" && exit 0

#
arm_type="unknown ARM system"
arm_type="? (unknown ARM system)"
rpi_revision=`sed -rn 's/Revision\s+\:\s+([0-9a-z_\-\s\,\(\)]+)/\1/p' /proc/cpuinfo`

if [[ $rpi_revision == *"900092"* ]]; then
Expand All @@ -19,7 +19,7 @@ elif [[ $rpi_revision == *"a02082"* || $rpi_revision = *"a22082"* ]]; then
arm_type="Raspberry Pi 3"
fi

echo ">>> Starting installation on $arm_type"
echo ">>> Starting installation on ARM device compatible with $arm_type"

# Download and install IPFS
ipfs_arch=${2-"linux-arm"}
Expand All @@ -44,33 +44,28 @@ sudo chmod 755 $ipfs_destination
# Maybe initialize IPFS
[ ! -d ~/.ipfs ] && ipfs init

# Maybe install and enable bring-up configurations for IPFS daemon
if [ ! -f /lib/systemd/system/ipfs-daemon.service ]; then
cat << EOF | sudo tee /lib/systemd/system/ipfs-daemon.service
[Unit]
Description=IPFS daemon
Wants=network.target
After=network.target
[Service]
Type=simple
Environment=IPFS_PATH=$HOME/.ipfs
ExecStart=/usr/local/bin/ipfs daemon
ExecStop=/usr/bin/pkill -f ipfs
Restart=on-failure
RestartSec=10s
[Install]
WantedBy=multi-user.target
EOF

# Install and enable bring-up configurations for IPFS daemon
init_system=$(get_init_system)
ipfs_path=$HOME/.ipfs

if [ $init_system == "systemd" ]; then
cat ./templates/ipfs-daemon.service.tpl | sed \
"s|{{ipfs_path}}|${ipfs_path}|g" | \
sudo tee /lib/systemd/system/ipfs-daemon.service > /dev/null

sudo systemctl daemon-reload
sudo systemctl enable ipfs-daemon
sudo systemctl start ipfs-daemon.service
elif [ $init_system == "upstart" ]; then
cat ./templates/ipfs-daemon.conf.tpl | sed \
"s|{{ipfs_path}}|${ipfs_path}|g" | \
sudo tee /etc/init/ipfs-daemon.conf > /dev/null

sudo initctl reload-configuration
sudo service ipfs-daemon start
else
echo ">> Unable to detect init system - you don't seem to be using systemd or upstart. The IPFS daemon will have to be controlled manually."
fi

# Start IPFS daemon
sudo systemctl daemon-reload
sudo systemctl enable ipfs-daemon
sudo systemctl start ipfs-daemon.service

#
echo ">>> All done."

19 changes: 19 additions & 0 deletions lib/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# Get init system used by OS (upstart, systemd, etc.)
function get_init_system() {
init_from_process=$(sudo ps -p 1 | tail -n 1 | cut -d ":" -f 3 | cut -d " " -f 2)

[[ $init_from_process == *"systemd"* ]] && echo "systemd" && return 1
[[ $init_from_process == *"upstart"* ]] && echo "upstart" && return 1

[ -x "$(command -v dpkg)" ] &&
init_from_package_manager=$(sudo dpkg -S /sbin/init | cut -d ":" -f 1) ||
init_from_package_manager=$(sudo rpm -qf /sbin/init)

[[ $init_from_package_manager == *"systemd"* ]] && echo "systemd" && return 1
[[ $init_from_package_manager == *"upstart"* ]] && echo "upstart" && return 1

return 0
}

16 changes: 16 additions & 0 deletions templates/ipfs-daemon.conf.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
description "IPFS daemon"

env IPFS_PATH={{ipfs_path}}

start on runlevel [2345]
stop on runlevel [!2345]

script
echo $$ > /var/run/ipfs-daemon.pid
exec /usr/local/bin/ipfs daemon
end script

pre-stop script
rm /var/run/ipfs-daemon.pid
end script

16 changes: 16 additions & 0 deletions templates/ipfs-daemon.service.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=IPFS daemon
Wants=network.target
After=network.target

[Service]
Type=simple
Environment=IPFS_PATH={{ipfs_path}}
ExecStart=/usr/local/bin/ipfs daemon
ExecStop=/usr/bin/pkill -f ipfs
Restart=on-failure
RestartSec=10s

[Install]
WantedBy=multi-user.target

50 changes: 34 additions & 16 deletions uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,58 @@

set -o pipefail

#
source ./lib/functions.sh

while getopts ":y" opt; do
case ${opt} in
y) assume_yes=true;;
esac
done

#
if [ -z $assume_yes ]; then
read -p "Uninstall IPFS? [Y/n] " proceed
[ ! "${proceed,,}" == "y" ] && exit 0
fi

#
echo ">>> Uninstalling IPFS"

sudo systemctl status ipfs-daemon.service > /dev/null
if [ $? -eq 0 ]; then
echo "Stopping IPFS service..."
sudo systemctl stop ipfs-daemon.service
if [ $? -ne 0 ]; then
echo "Failed to stop IPFS"
exit $?
# Stop IPFS daemon service
init_system=$(get_init_system)

if [ $init_system == "systemd" ]; then
sudo systemctl status ipfs-daemon.service 2>/dev/null 1>&2

if [ $? -eq 0 ]; then
echo ">>> Stopping IPFS service..."
sudo systemctl stop ipfs-daemon.service 2>/dev/null 1>&2

if [ $? -ne 0 ]; then
echo ">>> Failed to stop IPFS"
exit $?
fi

sudo systemctl disable ipfs-daemon 2>/dev/null 1>&2
sudo rm -f /lib/systemd/system/ipfs-daemon.service
sudo systemctl daemon-reload
fi
elif [ $init_system == "upstart" ]; then
sudo service ipfs-daemon status 2>/dev/null 1>&2

sudo systemctl disable ipfs-daemon > /dev/null
if [ $? -eq 0 ]; then
echo ">>> Stopping IPFS service..."
sudo service ipfs-daemon stop 2>/dev/null 1>&2

if [ $? -ne 0 ]; then
echo ">>> Failed to stop IPFS"
exit $?
fi

sudo rm -f /etc/init/ipfs-daemon.conf
sudo initctl reload-configuration
fi
fi

sudo rm -rf /usr/local/bin/ipfs
sudo rm -f /lib/systemd/system/ipfs-daemon.service
sudo systemctl daemon-reload > /dev/null

#
ipfs_dir=~/.ipfs

if [ -d $ipfs_dir ]; then
Expand All @@ -51,6 +70,5 @@ if [ -d $ipfs_dir ]; then
fi
fi

#
echo ">>> All done."

0 comments on commit d77046e

Please sign in to comment.