From d870e512e460c5f73659dc623d4947e4c862ee46 Mon Sep 17 00:00:00 2001 From: BianTanggui Date: Fri, 5 Jun 2020 17:10:41 +0800 Subject: [PATCH] Match-id-166316a638ef119d6bcf9222fc5d52f1b73f6ada --- build/build.sh | 16 +- install/deb/scripts/postinst | 2 +- install/deb/scripts/prerm | 15 + install/deb/src/main.c | 374 +++++++++++++++---------- install/rpm/ascend-docker-plgugin.spec | 15 +- 5 files changed, 268 insertions(+), 154 deletions(-) create mode 100644 install/deb/scripts/prerm diff --git a/build/build.sh b/build/build.sh index 422bc15..493b486 100644 --- a/build/build.sh +++ b/build/build.sh @@ -74,29 +74,35 @@ mkdir -pv {${DEBDIR},${BINDIR}} /bin/cp -f {${RUNTIMESRCDIR},${HOOKSRCDIR},${INSTALLHELPERSRCDIR},${CLISRCDIR}}/build/ascend-docker* ${BINDIR} CONPATH=`find ${INSTALLHELPERDIR} -name "control"` INSTPATH=`find ${INSTALLHELPERDIR} -name "postinst"` -/bin/cp -f ${CONPATH} ${INSTPATH} ${DEBDIR} +RMPATH=`find ${INSTALLHELPERDIR} -name "prerm"` +/bin/cp -f ${CONPATH} ${INSTPATH} ${RMPATH} ${DEBDIR} echo ${INSTPATH} chmod 555 ${DEBDIR}/postinst +chmod 555 ${DEBDIR}/prerm dpkg-deb -b ${DEBPACK} ascenddockertool_1.0.0_i386.deb DEBS=`find ${BUILD} -name "*.deb"` /bin/cp ${DEBS} ${OUTPUT} } +funcfillarch(){ +CPUARCH=`uname -m` +sed -i "4a\BuildArch: $CPUARCH" ${RPMSPECDIR}/*.spec +} + funcmakerpm(){ mkdir -pv ${RPMPACK}/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} /bin/cp -f {${RUNTIMESRCDIR},${HOOKSRCDIR},${INSTALLHELPERSRCDIR},${CLISRCDIR}}/build/ascend-docker* ${RPMSOURCESDIR} SPECPATH=`find ${INSTALLHELPERDIR} -name "*.spec"` dos2unix ${SPECPATH} /bin/cp -f ${SPECPATH} ${RPMSPECDIR} +funcfillarch rpmbuild --define "_topdir ${RPMPACK}" rpmbuild --showrc | grep topdir echo ${RPMPACK} echo "%_topdir ${RPMPACK}" > ~/.rpmmacros rpmbuild -bb ${RPMPACK}/SPECS/ascend-docker-plgugin.spec RPMS=`find ${RPMPACK} -name "*.rpm"` -ARCH=`uname -m` -RPMSNAME=${RPMS##*/} -/bin/cp ${RPMS} ${OUTPUT}/${RPMSNAME}.${ARCH} +/bin/cp ${RPMS} ${OUTPUT} } funcmakeclean(){ @@ -125,4 +131,4 @@ fi funcmakeunzip funcbuild funcmakerpm -#funcmakedeb +#funcmakedeb \ No newline at end of file diff --git a/install/deb/scripts/postinst b/install/deb/scripts/postinst index b7b36a6..f336a23 100644 --- a/install/deb/scripts/postinst +++ b/install/deb/scripts/postinst @@ -6,7 +6,7 @@ fi SRC="${DIR}/daemon.json.${PPID}" DST="${DIR}/daemon.json" BINDIR=/usr/local/bin -${BINDIR}/ascend-docker-plugin-install-helper ${DST} ${SRC} +${BINDIR}/ascend-docker-plugin-install-helper add ${DST} ${SRC} if [ "$?" != "0" ]; then echo "create damon.json failed\n" exit 1 diff --git a/install/deb/scripts/prerm b/install/deb/scripts/prerm new file mode 100644 index 0000000..089686d --- /dev/null +++ b/install/deb/scripts/prerm @@ -0,0 +1,15 @@ +#!/bin/bash +DIR=/etc/docker +if [ ! -d "${DIR}" ]; then +mkdir ${DIR} +fi +SRC="${DIR}/daemon.json.${PPID}" +DST="${DIR}/daemon.json" +BINDIR=/usr/local/bin +${BINDIR}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} +if [ "$?" != "0" ]; then +echo "del damon.json failed\n" +exit 1 +fi +\mv ${SRC} ${DST} +echo "del damom.json success\n" diff --git a/install/deb/src/main.c b/install/deb/src/main.c index 04da23e..3bf11ad 100644 --- a/install/deb/src/main.c +++ b/install/deb/src/main.c @@ -1,147 +1,229 @@ -/* - * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. - * Description: ascend_docker_install工具,用于辅助用户安装ascend_docker -*/ -#include "cJSON.h" -#include -#include -#include - -#define MAX_JSON_FILE_SIZE 65535 -#define NUM_ARGS 3 -#define FINAL_FILE_INDEX 1 -#define TEMP_FILE_INDEX 2 -#define PATH_VALUE "/usr/local/bin/ascend-docker-runtime" - -void ReadJsonFile(const FILE *pf, char *text, int maxBufferSize) -{ - fseek(pf, 0, SEEK_END); - - int size = (int)ftell(pf); - if (size >= maxBufferSize) { - fprintf(stderr, "file size too large\n"); - return; - } - - fseek(pf, 0, SEEK_SET); - fread(text, sizeof(char), size, pf); - text[size] = '\0'; -} - -int CreateNode(cJSON *runtimes) -{ - cJSON *node = cJSON_GetObjectItem(runtimes, "ascend"); - if (node != NULL) { - return 0; - } - - cJSON *newItem = NULL; - newItem = cJSON_CreateObject(); - if (newItem == NULL) { - return -1; - } - cJSON_AddItemToObject(runtimes, "ascend", newItem); - - cJSON *paraArray = NULL; - paraArray = cJSON_CreateArray(); - if (paraArray == NULL) { - return -1; - } - - cJSON_AddItemToObject(newItem, "path", cJSON_CreateString(PATH_VALUE)); - cJSON_AddItemToObject(newItem, "runtimeArgs", paraArray); - - return 0; -} - -int CreateRuntimes(cJSON *root) -{ - cJSON *runtimes = cJSON_CreateObject(); - if (runtimes == NULL) { - return -1; - } - cJSON_AddItemToObject(root, "runtimes", runtimes); - return CreateNode(runtimes); -} - -cJSON *CreateContent() -{ - cJSON *root = NULL; - root = cJSON_CreateObject(); - if (root == NULL) { - return NULL; - } - - int ret = CreateRuntimes(root); - if (ret != 0) { - return NULL; - } - - return root; -} - -cJSON *InsertContent(const FILE *pf) -{ - char jsonStr[MAX_JSON_FILE_SIZE] = {0x0}; - ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE); - cJSON *root = NULL; - root = cJSON_Parse(jsonStr); - if (!root) { - fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr()); - return NULL; - } - - cJSON *runtimes = NULL; - runtimes = cJSON_GetObjectItem(root, "runtimes"); - int ret; - if (runtimes == NULL) { - ret = CreateRuntimes(root); - } else { - ret = CreateNode(runtimes); - } - if (ret != 0) { - return NULL; - } - return root; -} - - -int DetectAndCreateJsonFile(const char *filePath, const char *tempPath) -{ - cJSON *root = NULL; - FILE *pf = NULL; - pf = fopen(filePath, "r+"); - if (pf == NULL) { - root = CreateContent(); - } else { - root = InsertContent(pf); - fclose(pf); - } - - if (root == NULL) { - fprintf(stderr, "error: failed to create json\n"); - return -1; - } - - pf = fopen(tempPath, "w"); - if (pf == NULL) { - fprintf(stderr, "error: failed to create file\n"); - return -1; - } - - fprintf(pf, "%s", cJSON_Print(root)); - fclose(pf); - - cJSON_Delete(root); - - return 0; -} - -/* 该函数只负责生成json.bak文件,由调用者进行覆盖操作 */ -int main(int argc, char *argv[]) -{ - if (argc != NUM_ARGS) { - return -1; - } - return DetectAndCreateJsonFile(argv[FINAL_FILE_INDEX], argv[TEMP_FILE_INDEX]); +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: ascend_docker_install工具,用于辅助用户安装ascend_docker +*/ +#include "cJSON.h" +#include +#include +#include + +#define MAX_JSON_FILE_SIZE 65535 +#define NUM_ARGS 4 +#define ADD_CMD "add" +#define RM_CMD "rm" +#define CMD_INDEX 1 +#define FINAL_FILE_INDEX 2 +#define TEMP_FILE_INDEX 3 +#define PATH_VALUE "/usr/local/bin/ascend-docker-runtime" + +void ReadJsonFile(const FILE *pf, char *text, int maxBufferSize) +{ + fseek(pf, 0, SEEK_END); + + int size = (int)ftell(pf); + if (size >= maxBufferSize) { + fprintf(stderr, "file size too large\n"); + return; + } + + fseek(pf, 0, SEEK_SET); + fread(text, sizeof(char), size, pf); + text[size] = '\0'; +} + +int CreateNode(cJSON *runtimes) +{ + cJSON *node = cJSON_GetObjectItem(runtimes, "ascend"); + if (node != NULL) { + return 0; + } + + cJSON *newItem = NULL; + newItem = cJSON_CreateObject(); + if (newItem == NULL) { + return -1; + } + cJSON_AddItemToObject(runtimes, "ascend", newItem); + + cJSON *paraArray = NULL; + paraArray = cJSON_CreateArray(); + if (paraArray == NULL) { + return -1; + } + + cJSON_AddItemToObject(newItem, "path", cJSON_CreateString(PATH_VALUE)); + cJSON_AddItemToObject(newItem, "runtimeArgs", paraArray); + + return 0; +} + +int CreateRuntimes(cJSON *root) +{ + cJSON *runtimes = cJSON_CreateObject(); + if (runtimes == NULL) { + return -1; + } + cJSON_AddItemToObject(root, "runtimes", runtimes); + return CreateNode(runtimes); +} + +cJSON *CreateContent() +{ + cJSON *root = NULL; + root = cJSON_CreateObject(); + if (root == NULL) { + return NULL; + } + + int ret = CreateRuntimes(root); + if (ret != 0) { + return NULL; + } + + return root; +} + +cJSON *InsertContent(const FILE *pf) +{ + char jsonStr[MAX_JSON_FILE_SIZE] = {0x0}; + ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE); + cJSON *root = NULL; + root = cJSON_Parse(jsonStr); + if (!root) { + fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr()); + return NULL; + } + + cJSON *runtimes = NULL; + runtimes = cJSON_GetObjectItem(root, "runtimes"); + int ret; + if (runtimes == NULL) { + ret = CreateRuntimes(root); + } else { + ret = CreateNode(runtimes); + } + if (ret != 0) { + return NULL; + } + return root; +} + + +int DetectAndCreateJsonFile(const char *filePath, const char *tempPath) +{ + cJSON *root = NULL; + FILE *pf = NULL; + pf = fopen(filePath, "r+"); + if (pf == NULL) { + root = CreateContent(); + } else { + root = InsertContent(pf); + fclose(pf); + } + + if (root == NULL) { + fprintf(stderr, "error: failed to create json\n"); + return -1; + } + + pf = fopen(tempPath, "w"); + if (pf == NULL) { + fprintf(stderr, "error: failed to create file\n"); + return -1; + } + + fprintf(pf, "%s", cJSON_Print(root)); + fclose(pf); + + cJSON_Delete(root); + + return 0; +} + +cJSON *GetNewContent(const FILE *pf) +{ + char jsonStr[MAX_JSON_FILE_SIZE] = {0x0}; + ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE); + + cJSON *root = NULL; + root = cJSON_Parse(jsonStr); + if (!root) { + fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr()); + return NULL; + } + + cJSON *runtimes = NULL; + runtimes = cJSON_GetObjectItem(root, "runtimes"); + if (runtimes == NULL) { + fprintf(stderr, "no runtime key found\n"); + cJSON_Delete(root); + return NULL; + } + + cJSON *ascend = NULL; + ascend = cJSON_GetObjectItem(runtimes, "ascend"); + if (ascend == NULL) { + fprintf(stderr, "no ascend key found\n"); + cJSON_Delete(root); + return NULL; + } + + cJSON *removedItem = NULL; + removedItem = cJSON_DetachItemViaPointer(runtimes, ascend); + if (removedItem == NULL) { + fprintf(stderr, "remove runtime failed\n"); + cJSON_Delete(root); + return NULL; + } + + cJSON_Delete(removedItem); + return root; +} + +int CreateRevisedJsonFile(const char *filePath, const char *tempPath) +{ + FILE *pf = NULL; + pf = fopen(filePath, "r+"); + if (pf == NULL) { + fprintf(stderr, "error: no json files found\n"); + return -1; + } + cJSON *newContent = NULL; + newContent = GetNewContent(pf); + fclose(pf); + + if (newContent == NULL) { + fprintf(stderr, "error: failed to create json\n"); + return -1; + } + + pf = fopen(tempPath, "w"); + if (pf == NULL) { + fprintf(stderr, "error: failed to create file\n"); + cJSON_Delete(newContent); + return -1; + } + + fprintf(pf, "%s", cJSON_Print(newContent)); + fclose(pf); + + cJSON_Delete(newContent); + + return 0; +} + +/* 该函数只负责生成json.bak文件,由调用者进行覆盖操作 */ +int main(int argc, char *argv[]) +{ + if (argc != NUM_ARGS) { + return -1; + } + printf("%s\n", argv[FINAL_FILE_INDEX]); + printf("%s\n", argv[TEMP_FILE_INDEX]); + printf("%s\n", argv[CMD_INDEX]); + if (strcmp(argv[CMD_INDEX], ADD_CMD) == 0) { + return DetectAndCreateJsonFile(argv[FINAL_FILE_INDEX], argv[TEMP_FILE_INDEX]); + } + return CreateRevisedJsonFile(argv[FINAL_FILE_INDEX], argv[TEMP_FILE_INDEX]); + } \ No newline at end of file diff --git a/install/rpm/ascend-docker-plgugin.spec b/install/rpm/ascend-docker-plgugin.spec index adb0179..495dbe9 100644 --- a/install/rpm/ascend-docker-plgugin.spec +++ b/install/rpm/ascend-docker-plgugin.spec @@ -1,7 +1,6 @@ Name: ascenddockerplugin Version: 1.0.0 Release: 1 -BuildArch: noarch Summary: simple RPM package License: FIXME @@ -29,7 +28,7 @@ BINDIR=/usr/local/bin if [ ! -d "${DIR}" ]; then mkdir ${DIR} fi -${BINDIR}/ascend-docker-plugin-install-helper ${DST} ${SRC} +${BINDIR}/ascend-docker-plugin-install-helper add ${DST} ${SRC} if [ "$?" != "0" ]; then echo "create damon.json failed\n" exit 1 @@ -38,6 +37,18 @@ fi echo "create damom.json success\n" %preun +#!/bin/bash +DIR=/etc/docker +BINDIR=/usr/local/bin +SRC="${DIR}/daemon.json.${PPID}" +DST="${DIR}/daemon.json" +${BINDIR}/ascend-docker-plugin-install-helper rm ${DST} ${SRC} +if [ "$?" != "0" ]; then +echo "del damon.json failed\n" +exit 1 +fi +\mv ${SRC} ${DST} +echo "del damom.json success\n" %postun