Skip to content

Instantly share code, notes, and snippets.

@daiooo
Last active March 30, 2020 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daiooo/2f6f05a8bc9d240bf626671939bc9dca to your computer and use it in GitHub Desktop.
Save daiooo/2f6f05a8bc9d240bf626671939bc9dca to your computer and use it in GitHub Desktop.
sense
#!/bin/bash
lsb_release -d
# check os version
co=$(lsb_release -a)
version="Ubuntu 18.04.4 LTS"
result=$(echo $co | grep "${version}")
if [[ "$result" != "" ]]; then
echo $version
else
echo -e "\n\033[31mError: OS requirement: $version\033[0m\n"
exit 0
fi
co=$(whoami)
version="root"
result=$(echo $co | grep "${version}")
if [[ "$result" != "" ]]; then
echo $version
else
echo -e "\n\033[31mError: User requirement: $version (su root)\033[0m\n"
exit 0
fi
#su root # >> cause not output
cd ~
# hosts
sed -i "1i 127.0.0.1 web.sense" /etc/hosts
sed -i "1i 127.0.0.1 api.sense" /etc/hosts
sed -i "1i 127.0.0.1 web.sense.d3vv.com" /etc/hosts
sed -i "1i 127.0.0.1 api.sense.d3vv.com" /etc/hosts
sed -i "1i alias apt='apt -y'" ~/.bashrc
sed -i "1i alias a='/data/sh/api.sh;/data/sh/web.sh;/data/sh/task.sh'" ~/.bashrc
sed -i "1i alias sense='/data/sh/api.sh;/data/sh/web.sh;/data/sh/task.sh'" ~/.bashrc
sed -i "1i alias lt='tail -f /data/log/task.log'" ~/.bashrc
sed -i "1i alias slog='journalctl -f -u'" ~/.bashrc
source ~/.bashrc
#sources.list
echo "update sources.list"
cat >/etc/apt/sources.list <<\EOF
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
apt -y update;
apt -y upgrade;
apt -y install lrzsz unzip htop nginx
apt -y install python3-pip python3-venv
apt -y install build-essential cmake git pkg-config libssl-dev libgl1-mesa-glx
apt -y install libsm6 libxext6 libxrender-dev
apt-key adv --keyserver keys.gnupg.net --recv-key F6E65AC044F831AC80A06380C8B3A55A6F3EFCDE || apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-key F6E65AC044F831AC80A06380C8B3A55A6F3EFCDE
add-apt-repository "deb http://realsense-hw-public.s3.amazonaws.com/Debian/apt-repo bionic main" -u
apt -y update
apt -y install librealsense2-dkms
apt -y install librealsense2-utils
#floder
mkdir /data
mkdir /data/www
mkdir /data/log
mkdir /data/sh
mkdir /data/www/api_sense
mkdir /data/www/api_sense/LINEMOD
mkdir /data/www/web_sense
cat >/data/www/api_sense/.env <<\EOF
PROJECT_NAME=API-Sense
VERSION = 2020.03.22_17.17.28
DEBUG=True
TASK_DEBUG=True
APP_HOST=127.0.0.1
APP_PORT=20091
DB_HOST=127.0.0.1
DB_PORT=10091
TOKEN_USER_EXPIRE=60*60*24*7
SECRET_KEY=secret
EOF
# nginx
apt -y install nginx
cat >/etc/nginx/conf.d/sense.conf <<\EOF
server {
listen 80;
server_name api.sense api.sense.d3vv.com;
root /data/www/api_lims;
location / {
proxy_pass http://127.0.0.1:20091;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name web.sense web.sense.d3vv.com;
root /data/www/web_sense/dist;
location / {
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
}
EOF
nginx -s reload
# SSDB
apt -y install libtool
cd /data/
function install_ssdb() {
if [ -f "/data/ssdb/ssdbs" ]; then
echo 'ssdb server exist'
return
fi
wget --no-check-certificate https://github.com/ideawu/ssdb/archive/master.zip && unzip master && mv ssdb-master ssdb && rm -rf master.zip*
cd /data/ssdb/deps/snappy-* && autoreconf --force --install && ./configure && make
cd /data/ssdb/deps/jemalloc-* && autoreconf --force --install && ./configure && make
cd /data/ssdb/ && make && make install
mv ssdb-server ssdbs
mkdir data log backup conf
rm -rf ChangeLog LICENSE README.md Dockerfile Makefile build* var* version *.conf
}
install_ssdb
mkdir /data/ssdb/data/91
cat >/data/ssdb/conf/91 <<\EOF
work_dir = ../data/91
pidfile = ../data/91/ssdb.pid
server:
ip: 0.0.0.0
port: 10091
replication:
binlog: yes
sync_speed: -1
slaveof:
# to identify a master even if it moved(ip, port changed)
logger:
level: warn
output: ../log/91.log
rotate:
size: 1000000000
leveldb:
cache_size: 500
write_buffer_size: 64
compaction_speed: 1000
compression: yes
EOF
cat >/etc/systemd/system/ssdb.service <<\EOF
[Unit]
Description=SSDB-91
After=network.target
[Service]
WorkingDirectory=/data/ssdb
PIDFile=/data/ssdb/data/91/ssdb.pid
ExecStart=/data/ssdb/ssdbs -s restart -d /data/ssdb/conf/91
ExecStop=/data/ssdb/ssdbs -s stop -d /data/ssdb/conf/91
Type=forking
Restart=on-failure
RestartSec=10s
User=root
[Install]
WantedBy=multi-user.target
EOF
systemctl enable ssdb
systemctl restart ssdb
# python
pip3 install -i https://mirrors.aliyun.com/pypi/simple pip -U
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple
cd /data/www/api_sense
#python3 -m venv venv
#source venv/bin/activate
pip3 install -i https://mirrors.aliyun.com/pypi/simple pip -U
pip3 install schedule
pip3 install numpy Cython
pip3 install pypng scipy scikit-learn
pip3 install open3d
pip3 install scikit-image tqdm pykdtree
pip3 install opencv-python==3.3.0.10 opencv-contrib-python==3.3.0.10
pip3 install trimesh
pip3 install pyrealsense2
pip3 install pyssdb
pip3 install db3
pip3 install pydantic[email,typing_extensions,dotenv]
pip3 install munch
pip3 install requests
pip3 install gunicorn
pip3 install uvicorn
pip3 install fastapi
pip3 install python-multipart
pip3 install aiofiles
pip3 install tqdm
pip3 install loguru
pip3 install dictdiffer
pip3 install schedule
pip3 install dpath
pip3 install bcrypt
pip3 install passlib
pip3 install pillow
# system ctl
cat >/etc/systemd/system/api.service <<\EOF
[Unit]
Description=sense api
After=network.target
[Service]
PIDFile=/data/tmp/sense.api.pid
WorkingDirectory=/data/www/api_sense
ExecStart=/usr/local/bin/gunicorn main:app -w 4 -b 127.0.0.1:20091 -k uvicorn.workers.UvicornWorker --pid /data/tmp/sense.api.pid
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
Type=simple
Restart=on-failure
RestartSec=10s
User=root
[Install]
WantedBy=multi-user.target
EOF
systemctl enable api
systemctl restart api
# sh
cat >/data/sh/task.sh <<\EOF
ps aux | grep task.py | grep -v grep | awk '{print $2}'
ps aux | grep task.py | grep -v grep | awk '{print $2}' | xargs kill
echo ---
cd /data/www/api_sense; nohup /usr/bin/python3 /data/www/api_sense/task.py > /data/log/task.log 2>&1 &
echo --- ---
ps aux | grep task.py | grep -v grep | awk '{print $2}'
echo --- ---
EOF
cat >/data/sh/api.sh <<\EOF
rm -rf /data/tmp/git/;git clone --depth=1 https://daippp:1sZOAPenycATw4O8@code.aliyun.com/uav/api_sense.git "/data/tmp/git/";cd /data/www/api_sense/;cp -rf /data/tmp/git/. .;ls
systemctl restart api;
sleep 1;
echo -n 'sense api ';systemctl status api | grep -o -P 'Active:...*?\ ';
EOF
cat >/data/sh/web.sh <<\EOF
rm -rf /data/tmp/git/;git clone --depth=1 https://daippp:1sZOAPenycATw4O8@code.aliyun.com/uav/web_sense.git "/data/tmp/git/";cd /data/www/web_sense/;cp -rf /data/tmp/git/. .;ls
EOF
cat >/etc/rc.local <<\EOF
/data/sh/task.sh
EOF
chmod +x /etc/rc.local
chmod +x /data/sh/api.sh
chmod +x /data/sh/task.sh
chmod +x /data/sh/web.sh
/data/sh/web.sh
/data/sh/api.sh
cd /data/www/api_sense
/data/sh/api.sh
/data/sh/task.sh
echo "\n*** finished *** "
echo "usage:"
echo "step 1 (bash order): sense"
echo "step 2 (Google Chrome): http://web.sense/ \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment