4 Star 4 Fork 0

AliOS Things / dev_tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
dev_tool_linux.sh 12.92 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/env bash
help_usage(){
echo "usage: bash $0 [OPTIONS]"
echo
echo "AliOS Things development tools"
echo "Setup AliOS-Things development environment easily"
echo
echo "Options:"
echo " -h, --help show help message"
echo " -i, --install install docker software, pull docker image, and start AliOS-Things development environment"
echo " -s, --start start AliOS-Things development environment"
echo " -r, --remove remove docker image which have AliOS-Things development environment"
echo " -u, --uninstall uninstall docker software"
echo " -p, --password modify code server password"
}
check_port() {
TCPListeningnum=`netstat -an | grep ":$1 " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`
UDPListeningnum=`netstat -an | grep ":$1 " | awk '$1 == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l`
(( Listeningnum = TCPListeningnum + UDPListeningnum ))
if [ $Listeningnum == 0 ]; then
echo "0"
else
echo "1"
fi
}
random_range() {
echo $((RANDOM % ($2 - $1) + $1))
}
get_random_port() {
PORT=0
temp=0
while [ $PORT == 0 ]; do
temp=`random_range $1 $2`
temp2=`expr $temp + 1`
if [ `check_port $temp` == 0 ] && [ `check_port $temp2` == 0 ]; then
PORT=$temp
fi
done
echo "$PORT"
}
install_docker(){
echo "-- Start to install AliOS Things development environment, and it need you to input password during installation."
echo "-- It may take 5 to 30 minutes. Please wait patiently..."
if [ -z $(which curl) ];then
echo "-- Installing curl..."
sudo apt update && sudo apt install -y curl
if [ $? -eq 0 ];then
echo "-- Installed curl successfully"
else
echo "-- Install curl failed. "
echo "-- Try to install curl manually as below "
echo "sudo apt update && sudo apt install -y curl"
return 1
fi
fi
if [ -z $(which docker) ];then
echo "-- Installing docker..."
curl -fsSL https://get.docker.com | sudo bash -s docker --mirror Aliyun
if [ $? -eq 0 ];then
echo "-- Installed docker successfully"
else
echo "-- Install docker failed. Try to reinstall it..."
curl -fsSL https://get.docker.com | sudo bash -s docker --mirror Aliyun
if [ $? -eq 0 ];then
echo "-- Installed docker successfully"
else
echo "-- Install docker failed again. "
echo "-- Try to install docker manually according to "
echo "https://docs.docker.com/engine/install/ubuntu/"
return 1
fi
fi
fi
cur_user=$SUDO_USER
if [ -z $cur_user ]; then
cur_user=$(whoami)
fi
echo "Current user is $cur_user"
sudo usermod -aG docker $cur_user
return 0
}
uninstall_docker(){
if [ -z $(which docker) ];then
echo "-- docker is not installed."
else
echo "-- Uninstalling docker..."
stop_docker
sudo apt purge -y docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
#cur_user=$(whoami)
#sudo gpasswd docker -d $cur_user
fi
}
start_docker(){
if [ -z $(ps -e | grep dockerd -o) ];then
echo "-- Starting dockerd, if necessary..."
sudo service docker start || exit
sleep 1
i=0
while ! ps -e | grep dockerd -o; do
(( i++ == 0 )) && printf %s "-- Waiting for Docker to finish starting up..." || printf "."
sleep 1
done
(( i )) && printf "\n"
echo "-- Docker is ready."
fi
return 0
}
stop_docker(){
if [ -n $(ps -e | grep dockerd -o) ];then
echo "-- Quitting dockerd, if running..."
sudo service docker stop
echo "-- Docker is stopped."
echo "-- Restarting it too quickly can cause errors."
else
echo "-- Docker is not started."
fi
return 0
}
pull_image(){
echo "-- Starting pull docker image..."
sudo docker pull registry.cn-shanghai.aliyuncs.com/alios-things/rtos:v0.1
if [ $? -eq 0 ];then
echo "-- Pulled docker image successfully"
else
echo "-- Pulled docker image failed. Try to pull it again..."
sudo docker pull registry.cn-shanghai.aliyuncs.com/alios-things/rtos:v0.1
if [ $? -eq 0 ];then
echo "-- Pulled docker image successfully"
else
echo "-- Pull docker image failed again. "
echo "-- Try to start a new terminal and download it manually as below "
echo "docker pull registry.cn-shanghai.aliyuncs.com/alios-things/rtos:v0.1"
return 1
fi
fi
return 0
}
check_container(){
containers=($(sudo docker ps -a | grep -E alios-docker_[0-9]+ -o))
return ${#containers[*]}
}
get_container(){
containers=($(sudo docker ps -a | grep -E alios-docker_[0-9]+ -o))
echo "${containers[*]}"
}
get_running_container(){
containers=($(sudo docker ps -f status=running | grep -E alios-docker_[0-9]+ -o))
echo "${containers[*]}"
}
remove_image(){
check_container
if [ $? -gt 0 ];then
container_arr=(`get_container`)
echo "-- remove docker container: ${container_arr[*]}"
sudo docker stop ${container_arr[*]} && sudo docker rm ${container_arr[*]}
if [ $? -ne 0 ];then
echo "-- Failed. Please exit the docker container ${container_arr[*]}, and retry it. "
return 1
fi
fi
if [ -n $(docker images | grep alios_things -o) ];then
echo "-- remove docker image: registry.cn-shanghai.aliyuncs.com/alios-things/rtos:v0.1"
sudo docker image rm registry.cn-shanghai.aliyuncs.com/alios-things/rtos:v0.1
if [ $? -ne 0 ];then
echo "-- Failed. Try to remove it manually as below "
echo "docker image rm registry.cn-shanghai.aliyuncs.com/alios-things/rtos:v0.1"
return 1
fi
fi
return 0
}
create_volume(){
volume=$(sudo docker volume ls | grep alios_docker_workspace -o)
if [ -z "$volume" ];then
sudo docker volume create --name alios_docker_workspace
fi
}
install_chrome(){
if [ -z $(which google-chrome) ];then
echo "-- Installing chrome..."
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
&& sudo dpkg -i google-chrome*; sudo apt-get -f install
if [ $? -eq 0 ];then
echo "-- Installed chrome successfully"
rm -f google-chrome-stable_current_amd64.deb
else
echo "-- Install chrome failed. "
echo "-- Try to install chrome manually as below "
echo "wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && sudo dpkg -i google-chrome*; sudo apt-get -f install"
return 1
fi
fi
return 0
}
run_docker_first_time()
{
no_web=$1
port=`get_random_port 49152 65530`
port2=`expr $port + 1`
echo "-- Docker alios-docker_$port run for first time..."
if [ -f $HOME/.ssh/id_rsa.pub ]; then
cp -f $HOME/.ssh/id_rsa.pub $HOME/authorized_keys
fi
create_volume
echo "#!/bin/bash" > $HOME/autorun.sh
echo "if [ ! -d /workspace/AliOS-Things ];then" >> $HOME/autorun.sh
echo "cd /workspace && git clone --depth 1 https://gitee.com/alios-things/AliOS-Things.git -b dev_3.1.0_haas" >> $HOME/autorun.sh
echo "else" >> $HOME/autorun.sh
echo "cd /workspace/AliOS-Things && git pull" >> $HOME/autorun.sh
echo "fi" >> $HOME/autorun.sh
echo "echo \"export AOS_SDK_PATH=/workspace/AliOS-Things\" >> /root/.bashrc" >> $HOME/autorun.sh
echo "cd /docker_share && touch git_clone_done" >> $HOME/autorun.sh
sudo docker run -dt --name alios-docker_$port -p $port:22 -p $port2:8080 -v $HOME/:/docker_share/ -v alios_docker_workspace:/workspace registry.cn-shanghai.aliyuncs.com/alios-things/rtos:v0.1 bash
if [ $? -eq 0 ];then
echo "-- Docker alios-docker_$port run successfully"
sleep 1
rm -f $HOME/authorized_keys
i=0
while [ ! -f $HOME/git_clone_done ]; do
(( i++ == 0 )) && printf %s "-- Waiting for downloading AliOS Things source code..." || printf "."
sleep 1
done
(( i )) && printf "\n"
rm -f $HOME/git_clone_done $HOME/autorun.sh
echo "Congratulations! The development environment is successfully installed."
echo "You can open a browser and start your experience journey."
echo "code server address: http://127.0.0.1:$port2/?folder=/workspace/AliOS-Things"
pwd_line=$(sudo docker exec alios-docker_$port bash -c "cat /root/.config/code-server/config.yaml | grep \"password:\"")
echo $pwd_line
echo "Please start a new terminal to modify password with option -p, or --password as soon as possible."
if [ -n $no_web ] && [ "$no_web" != "no_web" ]; then
if [ -z $(which xclip) ];then
sudo apt update && sudo apt install xclip
fi
sleep 2
echo $pwd_line | sed "s/password: //" | xclip -selection c
google-chrome "http://127.0.0.1:$port2/?folder=/workspace/AliOS-Things" >/dev/null 2>&1 &
fi
else
rm -f $HOME/authorized_keys $HOME/autorun.sh
echo "-- Docker alios-docker_$port run failed. "
return 1
fi
return 0
}
run_docker()
{
no_web=$1
if ! install_chrome; then exit 1; fi
check_container
if [ $? -eq 0 ];then
if ! run_docker_first_time $no_web; then return 1; fi
return 0
fi
running_container_arr=(`get_running_container`)
if [ ${#running_container_arr[*]} -eq 0 ];then
container_arr=(`get_container`)
echo "-- Docker ${container_arr[0]} run..."
port=$(echo ${container_arr[0]} | grep -E "[0-9]+" -o)
port2=`expr $port + 1`
sudo docker container start ${container_arr[0]}
if [ $? -eq 0 ];then
echo "-- Docker ${container_arr[0]} run successfully"
echo "-- code-server address http://127.0.0.1:$port2/?folder=/workspace/AliOS-Things"
if [ -n $no_web ] && [ "$no_web" != "no_web" ]; then
sleep 2
google-chrome "http://127.0.0.1:$port2/?folder=/workspace/AliOS-Things" >/dev/null 2>&1 &
fi
else
echo "-- Docker ${container_arr[0]} run failed again. "
return 1
fi
else
echo "-- Docker ${running_container_arr[0]} is already running..."
port=$(echo ${running_container_arr[0]} | grep -E "[0-9]+" -o)
port2=`expr $port + 1`
echo "-- code-server address http://127.0.0.1:$port2/?folder=/workspace/AliOS-Things"
if [ -n $no_web ] && [ "$no_web" != "no_web" ]; then
google-chrome "http://127.0.0.1:$port2/?folder=/workspace/AliOS-Things" >/dev/null 2>&1 &
fi
fi
return 0
}
update_password(){
running_container_arr=(`get_running_container`)
echo "-- Updating ${running_container_arr[0]} code server password..."
echo -n "Please Enter New Password:"
read -s PASSWORD1
echo
echo -n "Enter New Password Again:"
read -s PASSWORD2
echo
if [ "$PASSWORD1" == "$PASSWORD2" ]; then
sudo docker exec ${running_container_arr[0]} bash -c "sed -i \"s/password: [0-9a-zA-Z]*/password: $PASSWORD1/g\" /root/.config/code-server/config.yaml"
if [ $? -eq 0 ];then
echo "-- Set password successfully."
echo "-- Restarting the docker..."
sudo docker restart ${running_container_arr[0]}
if [ $? -ne 0 ];then
echo "-- Restart the docker failed. Try to start it manually as below:"
echo "bash ./dev_tool_linux.sh -s"
return 1
fi
echo "-- Restarted the docker successfully"
return 0
fi
fi
echo "-- Set password failed."
return 1
}
burn_firmware_server()
{
if [ -z $(which python) ];then
echo "-- python is not installed. Try to install it..."
sudo apt update && sudo apt install -y python
if [ $? -eq 0 ];then
echo "-- Installed python successfully"
else
echo "-- Install python failed. "
echo "-- Try to install python manually as below "
echo "sudo apt update && sudo apt install -y python"
return 1
fi
fi
num=$(ps -ef | grep "aos_burn_tool/aos_burn_tool.py" | grep -v grep | wc -l)
if [ $num -gt 0 ]; then
ps -ef | grep 'aos_burn_tool/aos_burn_tool.py' | grep -v grep | awk '{print $2}' | sudo xargs kill -9
fi
sudo echo ""
sudo python aos_burn_tool/aos_burn_tool.py &
if [ $? -eq 0 ];then
echo "-- Start flash programmer in backgroud successfully"
else
echo "-- Start flash programmer failed."
echo "-- If it's not started yet, try to run it as below "
echo "sudo python aos_burn_tool/aos_burn_tool.py"
return 1
fi
return 0
}
[[ $(uname) == "Linux" ]] || { echo "This function only runs on Linux." >&2; exit 2; }
case $1 in
-h|--help)
help_usage
;;
-i|--install)
if ! install_docker; then exit 1; fi
if ! start_docker; then exit 1; fi
if ! pull_image; then exit 1; fi
if ! run_docker; then exit 1; fi
if ! burn_firmware_server; then exit 1; fi
;;
-s|--start)
if ! start_docker; then exit 1; fi
if ! run_docker; then exit 1; fi
if ! burn_firmware_server; then exit 1; fi
;;
-u|--uninstall)
uninstall_docker
;;
-p|--password)
if ! start_docker; then exit 1; fi
if ! run_docker no_web; then exit 1; fi
if ! update_password; then exit 1; fi
;;
-r|--remove)
if ! start_docker; then exit 1; fi
remove_image
;;
*)
echo "invalid option \"$1\""
echo
help_usage
exit 1
esac
exit 0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/alios-things/dev_tool.git
git@gitee.com:alios-things/dev_tool.git
alios-things
dev_tool
dev_tool
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891