diff --git a/shell/json/Makefile b/shell/json/Makefile new file mode 100644 index 0000000..02b5561 --- /dev/null +++ b/shell/json/Makefile @@ -0,0 +1,21 @@ +CC=g++ -Wall -g +PWD=$(shell pwd) +PROG=json_shell + +vpath %.cpp ${PWD} ../../cpp/runner +LDFLAGS := -lboost_system -lboost_filesystem -lboost_thread -lpthread + +OBJS := json_shell.o task.o task_manager.o + +build: ${PROG} + +${PROG}: ${OBJS} + ${CC} -o $@ $^ ${LDFLAGS} + +%.o:%.cpp + ${CC} -c $< + +clean: + rm -f *.o ${PROG} + +rebuild: clean build diff --git a/shell/json/config_softwware.bash b/shell/json/config_softwware.bash new file mode 100755 index 0000000..a676da1 --- /dev/null +++ b/shell/json/config_softwware.bash @@ -0,0 +1,64 @@ +#!/bin/bash + +VALUES=$1 + +for s in $(echo $VALUES | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]"); do + export $s +done + +function configSoftware() { + local markSoft=$1 + markSoft=${markSoft//\[/} + markSoft=${markSoft//\]/} + local markList=(${markSoft//,/ }) + + local availSoft=$2 + availSoft=${availSoft//\[/} + availSoft=${availSoft//\]/} + local availList=(${availSoft//,/ }) + + declare -a removeList + idx=0 + for mark in "${markList[@]}"; do + found=0 + for avail in "${availList[@]}"; do + if [ $mark == $avail ]; then + found=1 + fi + done + if [ $found == 0 ]; then + removeList[$idx]=$mark + ((idx++)) + fi + done + + removeLen=${#removeList[@]} + if [ $removeLen != 0 ]; then + # remove + echo "Will remove: apt-get remove ${removeList[@]}" + # apt-get remove --allow-remove-essential ${removeList[@]} + fi + availLen=${#availList[@]} + if [ $availLen != 0 ]; then + # install + echo "Will install: apt-get install -y ${availList[@]}" + # apt-get install -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" ${availList[@]} + fi +} + +# MARK_SOFTWARES=`echo "$1" | jq -c '.MARK_SOFTWARES'` +# AVAILABLE_SOFTWARES=`echo "$1" | jq -c '.AVAILABLE_SOFTWARES'` +echo "Mark: ${MARK_SOFTWARES}" +echo "Avail: ${AVAILABLE_SOFTWARES}" + +if [ ! -z ${MARK_SOFTWARES} ]; then + echo "Has mark" +fi + +if [ ! -z ${AVAILABLE_SOFTWARES} ]; then + echo "Has avail" +fi + + if [ ! -z ${MARK_SOFTWARES} -a ! -z ${AVAILABLE_SOFTWARES} ]; then + configSoftware ${MARK_SOFTWARES} ${AVAILABLE_SOFTWARES} + fi diff --git a/shell/json/dmcg_software_diff.sh b/shell/json/dmcg_software_diff.sh new file mode 100755 index 0000000..574d994 --- /dev/null +++ b/shell/json/dmcg_software_diff.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +if [ "$1"x == ""x ]; then + echo "Please input mark and available json" + exit +fi + +if [ "$2"x == ""x ]; then + echo "Please input mark and available json" + exit +fi + +markRet=`cat $1 | jq -c '[.[]]'` +echo "Software Mark: ${markRet}" +markRet=${markRet//\[/} +markRet=${markRet//\]/} +markList=(${markRet//,/ }) + +availRet=`cat $2 | jq -c '[.available_softwares[]]'` +echo "Available List: ${availRet}" +availRet=${availRet//\[/} +availRet=${availRet//\]/} +availList=(${availRet//,/ }) + +function is_in_available_list() { + local name=$1 + local list=$2 + + for avail in "${availList[@]}"; do + echo -e "\tCompare: ${avail}, ${name}" + if [ "$avail"x == "$name"x ]; then + return 1 + fi + done + + return 0 +} + +declare -a removeList +idx=0 +for mark in "${markList[@]}"; do + echo -e "\nStart: ${mark}" + is_in_available_list ${mark} + ret=$? + echo "Ret: $ret, $idx" + if [ "${ret}"x == "0"x ]; then + removeList[$ret]=$mark + echo "After insert: ${removeList}" + ((idx++)) + fi +done + +removeLen=${#removeList[@]} +if [ $removeLen == 0 ]; then + echo "nothing to do" + exit +fi +echo "Remove len(${removeLen}): apt-get purge ${removeList[@]}" diff --git a/shell/json/json_shell.cpp b/shell/json/json_shell.cpp new file mode 100644 index 0000000..9fea2da --- /dev/null +++ b/shell/json/json_shell.cpp @@ -0,0 +1,94 @@ +// Compile: g++ -Wall -g +#include +#include +#include +#include + +#include + +#include "../../cpp/runner/task.h" +#include "../../cpp/runner/task_manager.h" + +using namespace std; + +static string +load_filename(const string &filename) +{ + string content; + try { + fstream fr; + fr.open(filename, ios::in); + istreambuf_iterator beg(fr); + istreambuf_iterator end; + content = string(beg, end); + } catch (const exception &e) { + cout<<"Load file failed: "< "< args = {values.dump(-1)}; + + namespace dmr = dmcg::module::runner; + dmr::TaskManager manager; + boost::shared_ptr task = manager.Create(argv[1], args); + if (!task) { + cout<<"Create task failed"<Finish.connect([task]( + const string &exception, + const int &exit_code, + const string &output, + const string &error_output + ){ + if (!exception.empty()) { + cout<<"Exception: "<Run(true); + + return 0; +} \ No newline at end of file diff --git a/shell/json/testdata/pc_config.json b/shell/json/testdata/pc_config.json new file mode 100644 index 0000000..8a2f700 --- /dev/null +++ b/shell/json/testdata/pc_config.json @@ -0,0 +1 @@ +{"pc_config":{"iptables":{"content":["iptables -A INPUT -p tcp --dport 80 -j DROP","iptables -A OUTPUT -p tcp --sport 80 -j DROP"]},"screensaver":{"enable":true,"delay":600},"power":{"display_standby":3600},"pam_cracklib":{"minlen":20,"reject_username":true,"minclass":0},"wallpaper":{"image":"/data/wallpaper/1557747710158231205.png"},"script":{"name":"empty.sh","content":"","upload_at":"2019-05-10T05:22:29Z"}},"available_softwares":["deepin-terminal","libghc-aeson-qq-dev","libghc-aeson-qq-doc"],"can_login":false} \ No newline at end of file diff --git a/shell/json/testdata/software_mark.json b/shell/json/testdata/software_mark.json new file mode 100644 index 0000000..3f67adb --- /dev/null +++ b/shell/json/testdata/software_mark.json @@ -0,0 +1 @@ +["deepin-terminal","libghc-aeson-qq-dev","libghc-aeson-qq-doc","parcellite"] \ No newline at end of file