Skip to content

Commit

Permalink
add helper / automation scripts (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Jun 3, 2020
1 parent 9fffa09 commit 279b479
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
72 changes: 72 additions & 0 deletions scripts/initialize-contributors-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Setup Script for Azure Ubuntu 18.04 LTS

## System Initialization

```sh
sudo apt update
sudo apt install jq

# BBR
sudo echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
sudo echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sudo sysctl -p

# Docker
sudo apt install docker.io
groupadd wechaty
setfacl -m group:wechaty:rw /var/run/docker.sock
mkdir -p /home/wechaty
cd /home/wechaty
tar czv /var/lib/docker - | tar xv
cd /var/lib
mv docker docker.bak
ln -s /home/var/lib/docker .

# Swap File
sudo dd if=/dev/zero of=/swapfile bs=1024 count=4096000
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

sudo cat <<_EOF_ > /etc/rc.local
#!/usr/bin/env bash
swapon /swapfile
_EOF_
sudo chmod +x /etc/rc.local
```

## Python 3

```sh
sudo apt install python3.8 python3.8-dev
sudo apt install python3-pip python-pip
sudo update-alternatives --config python3

sudo apt remove \
python3-apt \

sudo apt install \
python3-apt \
libglib2.0-dev \
libdbus-glib-1-dev \

# FIXME: gi https://stackoverflow.com/a/60352723/1123955

sudo pip3 install --upgrade \
netifaces \
distro \
dbus-python \
```

## Node.js 12

```sh
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs
```

## Add Contributor

```sh
sudo ...
```
79 changes: 79 additions & 0 deletions scripts/update-contributors-server-account.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash
#
# Copyright 2020 Huan (李卓桓) <zixia@zixia.net>
# Wechaty Contributors
#
set -eo pipefail

if [ -z "$GITHUB_TOKEN" ]; then
>&2 echo "GITHUB_TOKEN not set."
fi

function orgId () {
org=$1
curl -sH "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/orgs/$1" |\
jq '.id'
}

function teamId () {
org=$1
team=$2
curl -sH "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/orgs/$org/teams" |\
jq 'map(select(.name | match("'$team'";"i"))) | .[].id'
}

function members () {
org=$1
team=$2
curl -sH "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/organizations/$(orgId $org)/team/$(teamId $org $team)/members?per_page=100"
}

function memberId () {
org=$1
team=$2
login=$3
members "$org" "$team" |\
jq 'map(select(.login | match("^'$login'$";"i"))) | .[].id'
}

function memberLoginList () {
org=$1
team=$2
members "$org" "$team" |\
jq -r 'map(select(.login)) | .[].login'
}

function keys () {
login=$1
curl -sH "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/users/$login/keys |\
jq -r 'map(select(.key)) | .[].key'
}

for login in $(memberLoginList wechaty contributors); do
SSH_DIR="/home/$login/.ssh"
KEY_FILE="$SSH_DIR/authorized_keys"
KEY_PUB=$(keys $login)

cat<<_POD_
if [ ! -d "/home/$login" ]; then
useradd -m $login
fi
if [ ! -d "$SSH_DIR" ]; then
mkdir "$SSH_DIR"
chmod 700 "$SSH_DIR"
chown "$login"."$login" "$SSH_DIR"
fi
cat<<_EOF_ > "$KEY_FILE"
$KEY_PUB
_EOF_
chmod 0600 "$KEY_FILE"
chown "$login"."$login" "$KEY_FILE"
_POD_

done

0 comments on commit 279b479

Please sign in to comment.