From df9639dd675e99e9ecca2d967d0f19ac8555797a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=88=98=EC=A4=80?= Date: Wed, 29 Nov 2023 15:18:43 +0900 Subject: [PATCH] first commit --- README.md | 75 +++++++++++++++++++++++++++++++++++++++++++ app-setting.sh | 14 ++++++++ config1.json | 9 ++++++ config2.json | 6 ++++ install.sh | 9 ++++++ main.py | 24 ++++++++++++++ sdt-cloud-app.service | 10 ++++++ 7 files changed, 147 insertions(+) create mode 100644 README.md create mode 100755 app-setting.sh create mode 100644 config1.json create mode 100644 config2.json create mode 100755 install.sh create mode 100644 main.py create mode 100644 sdt-cloud-app.service diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c37f71 --- /dev/null +++ b/README.md @@ -0,0 +1,75 @@ +# SDTcloud App Format + +## SDTcloud 앱을 만들기 위한 앱 포맷입니다. +### 코드 구성 +```bash +sdtcloud-app-format +├── app-setting.sh +├── config1.json +├── config2.json +├── install.sh +├── main.py +├── README.md +└── sdt-cloud-app.service +``` +- 배포될 앱은 실행 파일, Config 파일, 앱 설치 shell script와 같이 필수 파일 있어야 합니다. +- app-setting.sh 파일은 앱 생성 기본 양식을 저장하고자 하는 이름의 앱으로 변경 시켜주는 스크립트 입니다. +- SDT cloud에서 배포 되는 앱은 리눅스의 Systemd에 의해서 관리됩니다. +- 앱을 배포하기 위해서는 실행 파일 이외에 .service 파일이 반드시 필요합니다. + +### 살행 코드 내용 +- SDT Cloud는 현재 Python과 Java 언어에 대해서 앱 배포가 가능합니다. +- 두 언어를 작성할 때, Config 파일을 읽을 위치를 코드 내에서 지정해야 합니다. +- SDT Cloud는 device에 아래 위치에 앱을 설치하고 관리합니다. +```bash +/usr/local/sdt/app +``` +- 즉, SDTcloud의 앱은 "/usr/local/sdt/app" 디렉토리에 보관되고 관리 되므로, "/usr/local/sdt/app/앱이름/config.json" 경로와 같이 config 파일을 읽도록 코드를 작성해야 합니다. +- 또한, 앱이름은 사용자가 배포할 때마다 앱의 이름이 변경될 수 있으므로 프로세스가 매개변수를 입력받아 동적으로 변경가능하도록 설계 해야 합니다. +```bash +## Python의 경우 +parser = argparse.ArgumentParser() +parser.add_argument('-app',help='') +args = parser.parse_args() + +with open(f'/usr/local/sdt/app/{args.app}/config.json', encoding='UTF-8') as f: + jsonData = json.load(f) +``` +- 따라서 python의 경우 앱을 실행할 때, 앱이름을 매개 변수로 입력 받아서 실행하도록 설계 되어야 합니다. +```bash +python3 main.py -app example-app +``` + +### 앱 생성 +- 배포하고자 하는 앱이 Python이며, 실행 파일은 main.py이고 config 파일이 config.json 이며, 이 앱의 이름을 example-app으로 앱을 저장할 것이라면, +- 앱을 저장하기 위해 다음 명령어로 셋팅해야 합니다. +```bash +$ mv config1.json config.json +$ vi config.json -> config 값 수정 +$ ./app-setting.sh example-app /usr/bin/python3 main.py +``` +- 3번 코드 라인 설명은 다음과 같습니다. + - example-app: 앱 이름 + - /usr/bin/python3: python3 bin 파일 위치 → 이 같은 경우는 정확한 bin 파일 경로로 작성해야 합니다. + - main.py: python3 실행 파일 +- 위 작업을 완료하면 앱이 SDT cloud를 통해 배포하기 위한 기본적인 앱 파일 구조가 완성됩니다. +- service 파일을 확인 하면 다음과 같습니다. + +```bash +$ vi example-app.service +[Unit] +Description=example-app + +[Service] +ExecStart=/usr/bin/python3 /usr/local/sdt/app/appName/main.py -app appName +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target +``` +- 이후, StackBase(gitea)에 앱을 업로드 합니다. +- 업로드 하기전 아래 작업을 수행 후, gitea에 새 저장소로 추가합니다. +```bash +$ rm -rf .git +``` \ No newline at end of file diff --git a/app-setting.sh b/app-setting.sh new file mode 100755 index 0000000..d880b91 --- /dev/null +++ b/app-setting.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +if [ -z $1 ] || [ -z $2 ] || [ -z $3 ] ; then + echo "Please fill in app's name, bin path, file." + exit +fi + + + +mv sdt-cloud-app.service $1.service +sed -i "s/sdt-cloud-app/$1/g" ./install.sh +sed -i "s/sdt-cloud-app/$1/g" ./$1.service +sed -i "s%exePath%$2%g" ./$1.service +sed -i "s/exeFile/$3/g" ./$1.service \ No newline at end of file diff --git a/config1.json b/config1.json new file mode 100644 index 0000000..f77c7a2 --- /dev/null +++ b/config1.json @@ -0,0 +1,9 @@ +{ + "topic": "performance", + "id": "sdt", + "password": "11223344", + "host": { + "address": "192.168.100.200", + "hostname": "sdt" + } +} \ No newline at end of file diff --git a/config2.json b/config2.json new file mode 100644 index 0000000..4a478a0 --- /dev/null +++ b/config2.json @@ -0,0 +1,6 @@ +{ + "hw": "intel", + "cpu": 12, + "memory": 32, + "gpu": "2080TI" +} diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..7f7fbd2 --- /dev/null +++ b/install.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -e + +sudo sed -i "s/appName/$1_$2/g" /usr/local/sdt/app/$1_$2/sdt-cloud-app.service +sudo cp /usr/local/sdt/app/$1_$2/sdt-cloud-app.service /etc/systemd/system/$1.service +sudo systemctl start $1 +sudo systemctl enable $1 + diff --git a/main.py b/main.py new file mode 100644 index 0000000..37ac664 --- /dev/null +++ b/main.py @@ -0,0 +1,24 @@ +import paho.mqtt.client as mqtt +import json +import argparse + +from datetime import datetime +import time +import yaml + +parser = argparse.ArgumentParser() +parser.add_argument('-app',help='') +args = parser.parse_args() + +with open(f'/usr/local/sdt/app/{args.app}/config.json', encoding='UTF-8') as f: + jsonData = json.load(f) + + +# mqttc = mqtt.Client() # puclisher 이름 +# mqttc.username_pw_set("sdt", "251327") +# mqttc.connect("13.209.39.139", 32259) +while True: + print(f"Config value: {jsonData['topic']}", flush=True) + time.sleep(2) + + diff --git a/sdt-cloud-app.service b/sdt-cloud-app.service new file mode 100644 index 0000000..a053e9c --- /dev/null +++ b/sdt-cloud-app.service @@ -0,0 +1,10 @@ +[Unit] +Description=sdt-cloud-app + +[Service] +ExecStart=exePath /usr/local/sdt/app/appName/exeFile -app appName +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target