-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·121 lines (102 loc) · 2.79 KB
/
install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# This script has two modes; one when run on the primary machine,
# and another to install on the secondary (remote) machine.
# Primary machine: ./install [user@]remotehost [/remote/path]
# Secondary machine: ./install . [/local/path]
# That is, if the first argument is a '.' character, then the
# script runs in secondary mode, installing to the local machine.
# Otherwise, the script creates SSH keys to the secondary machine,
# copies itself and its dependencies to that machine and then
# launches it with arguments "." "$2"
test_ssh() {
ssh -n $1 <<< true &>/dev/null
}
mksshkeys() {
# Requires interactivity
if [ $# -ne 1 ]
then
echo 'Usage: mksshkeys [user@]host'
exit 1
fi
if [ ! -d $HOME/.ssh ]
then
mkdir $HOME/.ssh
chmod 0755 $HOME/.ssh
fi
if [ ! -f $HOME/.ssh/id_rsa.pub ]
then
echo 'Generating RSA certificate'
ssh-keygen -f $HOME/.ssh/id_rsa -t rsa -a 100 -P ''
fi
echo "Installing ED25519 certificate on $1"
(
echo 'mkdir -p $HOME/.ssh ; chmod 0755 $HOME/.ssh ; '
echo 'cat << EOF >> $HOME/.ssh/authorized_keys'
cat $HOME/.ssh/id_rsa.pub
echo 'EOF'
echo 'chmod 0600 $HOME/.ssh/authorized_keys'
) | ssh $1
}
primary_mode() {
TARGET_HOST="$1"
TARGET_DIR="$2"
test_ssh $TARGET_HOST || mksshkeys $TARGET_HOST
scp "$0" ${TARGET_HOST}:/tmp
if ssh -tX ${TARGET_HOST} /tmp/$(basename $0) . "${TARGET_DIR}"
then
TMP=$(mktemp)
grep -v "^${TARGET_HOST} " ~/.upd-remote > $TMP
echo "${TARGET_HOST} ${TARGET_DIR:-~/src}" >> $TMP
mv $TMP ~/.upd-remote
else
echo "Install failed!"
fi
}
secondary_mode() {
if [ -z "$2" ]
then
TARGET_DIR="$(realpath ~/src)"
else
TARGET_DIR="$(realpath "$2")"
fi
echo "Creating $TARGET_DIR"
mkdir -p "$TARGET_DIR" || ( echo "Could not create target dir $TARGET_DIR"; exit 1 )
SUDOER=/etc/sudoers.d/upd-$(whoami)
until sudo -n true
do
echo Enter password on remote machine $(hostname) to allow passwordless sudo
sudo tee $SUDOER >/dev/null <<< "$(whoami) ALL=(ALL) NOPASSWD: ALL"
done
echo "Created $SUDOER"
if ! which git &>/dev/null
then
echo "Installing git"
sudo apt-get update
sudo apt-get -y install git
fi
cd "$TARGET_DIR"
echo "Checking out upd.git repo"
git clone https://github.com/jbreadner/upd.git
sudo tee /etc/profile.d/upd.sh <<< 'export PATH=$PATH:'$(pwd)'/upd'
for scriptlet in "${TARGET_DIR}"/upd/available/*
do
"$scriptlet" --install
done
echo "Cleaning up"
rm -f "$0"
echo "Installed"
exit 0
}
if [ $# -lt 1 -o $# -gt 2 -o "$1" = "-h" -o "$1" = "--help" ]
then
echo "Usage: ./install [user@]host [/remote/path]"
echo "Installs the 'upd' scripts on the remote host."
echo Called as: $0 "${@}"
exit 1
fi
if [ "$1" = "." ]
then
secondary_mode "$@"
else
primary_mode "$@"
fi