Match-id-166316a638ef119d6bcf9222fc5d52f1b73f6ada

This commit is contained in:
BianTanggui
2020-06-05 17:10:41 +08:00
parent 51c8fef96b
commit d870e512e4
5 changed files with 268 additions and 154 deletions

View File

@@ -74,29 +74,35 @@ mkdir -pv {${DEBDIR},${BINDIR}}
/bin/cp -f {${RUNTIMESRCDIR},${HOOKSRCDIR},${INSTALLHELPERSRCDIR},${CLISRCDIR}}/build/ascend-docker* ${BINDIR} /bin/cp -f {${RUNTIMESRCDIR},${HOOKSRCDIR},${INSTALLHELPERSRCDIR},${CLISRCDIR}}/build/ascend-docker* ${BINDIR}
CONPATH=`find ${INSTALLHELPERDIR} -name "control"` CONPATH=`find ${INSTALLHELPERDIR} -name "control"`
INSTPATH=`find ${INSTALLHELPERDIR} -name "postinst"` 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} echo ${INSTPATH}
chmod 555 ${DEBDIR}/postinst chmod 555 ${DEBDIR}/postinst
chmod 555 ${DEBDIR}/prerm
dpkg-deb -b ${DEBPACK} ascenddockertool_1.0.0_i386.deb dpkg-deb -b ${DEBPACK} ascenddockertool_1.0.0_i386.deb
DEBS=`find ${BUILD} -name "*.deb"` DEBS=`find ${BUILD} -name "*.deb"`
/bin/cp ${DEBS} ${OUTPUT} /bin/cp ${DEBS} ${OUTPUT}
} }
funcfillarch(){
CPUARCH=`uname -m`
sed -i "4a\BuildArch: $CPUARCH" ${RPMSPECDIR}/*.spec
}
funcmakerpm(){ funcmakerpm(){
mkdir -pv ${RPMPACK}/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS} mkdir -pv ${RPMPACK}/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
/bin/cp -f {${RUNTIMESRCDIR},${HOOKSRCDIR},${INSTALLHELPERSRCDIR},${CLISRCDIR}}/build/ascend-docker* ${RPMSOURCESDIR} /bin/cp -f {${RUNTIMESRCDIR},${HOOKSRCDIR},${INSTALLHELPERSRCDIR},${CLISRCDIR}}/build/ascend-docker* ${RPMSOURCESDIR}
SPECPATH=`find ${INSTALLHELPERDIR} -name "*.spec"` SPECPATH=`find ${INSTALLHELPERDIR} -name "*.spec"`
dos2unix ${SPECPATH} dos2unix ${SPECPATH}
/bin/cp -f ${SPECPATH} ${RPMSPECDIR} /bin/cp -f ${SPECPATH} ${RPMSPECDIR}
funcfillarch
rpmbuild --define "_topdir ${RPMPACK}" rpmbuild --define "_topdir ${RPMPACK}"
rpmbuild --showrc | grep topdir rpmbuild --showrc | grep topdir
echo ${RPMPACK} echo ${RPMPACK}
echo "%_topdir ${RPMPACK}" > ~/.rpmmacros echo "%_topdir ${RPMPACK}" > ~/.rpmmacros
rpmbuild -bb ${RPMPACK}/SPECS/ascend-docker-plgugin.spec rpmbuild -bb ${RPMPACK}/SPECS/ascend-docker-plgugin.spec
RPMS=`find ${RPMPACK} -name "*.rpm"` RPMS=`find ${RPMPACK} -name "*.rpm"`
ARCH=`uname -m` /bin/cp ${RPMS} ${OUTPUT}
RPMSNAME=${RPMS##*/}
/bin/cp ${RPMS} ${OUTPUT}/${RPMSNAME}.${ARCH}
} }
funcmakeclean(){ funcmakeclean(){
@@ -125,4 +131,4 @@ fi
funcmakeunzip funcmakeunzip
funcbuild funcbuild
funcmakerpm funcmakerpm
#funcmakedeb #funcmakedeb

View File

@@ -6,7 +6,7 @@ fi
SRC="${DIR}/daemon.json.${PPID}" SRC="${DIR}/daemon.json.${PPID}"
DST="${DIR}/daemon.json" DST="${DIR}/daemon.json"
BINDIR=/usr/local/bin BINDIR=/usr/local/bin
${BINDIR}/ascend-docker-plugin-install-helper ${DST} ${SRC} ${BINDIR}/ascend-docker-plugin-install-helper add ${DST} ${SRC}
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
echo "create damon.json failed\n" echo "create damon.json failed\n"
exit 1 exit 1

15
install/deb/scripts/prerm Normal file
View File

@@ -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"

View File

@@ -1,147 +1,229 @@
/* /*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* Description: ascend_docker_install工具用于辅助用户安装ascend_docker * Description: ascend_docker_install工具用于辅助用户安装ascend_docker
*/ */
#include "cJSON.h" #include "cJSON.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#define MAX_JSON_FILE_SIZE 65535 #define MAX_JSON_FILE_SIZE 65535
#define NUM_ARGS 3 #define NUM_ARGS 4
#define FINAL_FILE_INDEX 1 #define ADD_CMD "add"
#define TEMP_FILE_INDEX 2 #define RM_CMD "rm"
#define PATH_VALUE "/usr/local/bin/ascend-docker-runtime" #define CMD_INDEX 1
#define FINAL_FILE_INDEX 2
void ReadJsonFile(const FILE *pf, char *text, int maxBufferSize) #define TEMP_FILE_INDEX 3
{ #define PATH_VALUE "/usr/local/bin/ascend-docker-runtime"
fseek(pf, 0, SEEK_END);
void ReadJsonFile(const FILE *pf, char *text, int maxBufferSize)
int size = (int)ftell(pf); {
if (size >= maxBufferSize) { fseek(pf, 0, SEEK_END);
fprintf(stderr, "file size too large\n");
return; int size = (int)ftell(pf);
} if (size >= maxBufferSize) {
fprintf(stderr, "file size too large\n");
fseek(pf, 0, SEEK_SET); return;
fread(text, sizeof(char), size, pf); }
text[size] = '\0';
} fseek(pf, 0, SEEK_SET);
fread(text, sizeof(char), size, pf);
int CreateNode(cJSON *runtimes) text[size] = '\0';
{ }
cJSON *node = cJSON_GetObjectItem(runtimes, "ascend");
if (node != NULL) { int CreateNode(cJSON *runtimes)
return 0; {
} cJSON *node = cJSON_GetObjectItem(runtimes, "ascend");
if (node != NULL) {
cJSON *newItem = NULL; return 0;
newItem = cJSON_CreateObject(); }
if (newItem == NULL) {
return -1; cJSON *newItem = NULL;
} newItem = cJSON_CreateObject();
cJSON_AddItemToObject(runtimes, "ascend", newItem); if (newItem == NULL) {
return -1;
cJSON *paraArray = NULL; }
paraArray = cJSON_CreateArray(); cJSON_AddItemToObject(runtimes, "ascend", newItem);
if (paraArray == NULL) {
return -1; cJSON *paraArray = NULL;
} paraArray = cJSON_CreateArray();
if (paraArray == NULL) {
cJSON_AddItemToObject(newItem, "path", cJSON_CreateString(PATH_VALUE)); return -1;
cJSON_AddItemToObject(newItem, "runtimeArgs", paraArray); }
return 0; cJSON_AddItemToObject(newItem, "path", cJSON_CreateString(PATH_VALUE));
} cJSON_AddItemToObject(newItem, "runtimeArgs", paraArray);
int CreateRuntimes(cJSON *root) return 0;
{ }
cJSON *runtimes = cJSON_CreateObject();
if (runtimes == NULL) { int CreateRuntimes(cJSON *root)
return -1; {
} cJSON *runtimes = cJSON_CreateObject();
cJSON_AddItemToObject(root, "runtimes", runtimes); if (runtimes == NULL) {
return CreateNode(runtimes); return -1;
} }
cJSON_AddItemToObject(root, "runtimes", runtimes);
cJSON *CreateContent() return CreateNode(runtimes);
{ }
cJSON *root = NULL;
root = cJSON_CreateObject(); cJSON *CreateContent()
if (root == NULL) { {
return NULL; cJSON *root = NULL;
} root = cJSON_CreateObject();
if (root == NULL) {
int ret = CreateRuntimes(root); return NULL;
if (ret != 0) { }
return NULL;
} int ret = CreateRuntimes(root);
if (ret != 0) {
return root; return NULL;
} }
cJSON *InsertContent(const FILE *pf) return root;
{ }
char jsonStr[MAX_JSON_FILE_SIZE] = {0x0};
ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE); cJSON *InsertContent(const FILE *pf)
cJSON *root = NULL; {
root = cJSON_Parse(jsonStr); char jsonStr[MAX_JSON_FILE_SIZE] = {0x0};
if (!root) { ReadJsonFile(pf, &jsonStr[0], MAX_JSON_FILE_SIZE);
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr()); cJSON *root = NULL;
return NULL; root = cJSON_Parse(jsonStr);
} if (!root) {
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
cJSON *runtimes = NULL; return NULL;
runtimes = cJSON_GetObjectItem(root, "runtimes"); }
int ret;
if (runtimes == NULL) { cJSON *runtimes = NULL;
ret = CreateRuntimes(root); runtimes = cJSON_GetObjectItem(root, "runtimes");
} else { int ret;
ret = CreateNode(runtimes); if (runtimes == NULL) {
} ret = CreateRuntimes(root);
if (ret != 0) { } else {
return NULL; ret = CreateNode(runtimes);
} }
return root; if (ret != 0) {
} return NULL;
}
return root;
int DetectAndCreateJsonFile(const char *filePath, const char *tempPath) }
{
cJSON *root = NULL;
FILE *pf = NULL; int DetectAndCreateJsonFile(const char *filePath, const char *tempPath)
pf = fopen(filePath, "r+"); {
if (pf == NULL) { cJSON *root = NULL;
root = CreateContent(); FILE *pf = NULL;
} else { pf = fopen(filePath, "r+");
root = InsertContent(pf); if (pf == NULL) {
fclose(pf); root = CreateContent();
} } else {
root = InsertContent(pf);
if (root == NULL) { fclose(pf);
fprintf(stderr, "error: failed to create json\n"); }
return -1;
} if (root == NULL) {
fprintf(stderr, "error: failed to create json\n");
pf = fopen(tempPath, "w"); return -1;
if (pf == NULL) { }
fprintf(stderr, "error: failed to create file\n");
return -1; pf = fopen(tempPath, "w");
} if (pf == NULL) {
fprintf(stderr, "error: failed to create file\n");
fprintf(pf, "%s", cJSON_Print(root)); return -1;
fclose(pf); }
cJSON_Delete(root); fprintf(pf, "%s", cJSON_Print(root));
fclose(pf);
return 0;
} cJSON_Delete(root);
/* 该函数只负责生成json.bak文件由调用者进行覆盖操作 */ return 0;
int main(int argc, char *argv[]) }
{
if (argc != NUM_ARGS) { cJSON *GetNewContent(const FILE *pf)
return -1; {
} char jsonStr[MAX_JSON_FILE_SIZE] = {0x0};
return DetectAndCreateJsonFile(argv[FINAL_FILE_INDEX], argv[TEMP_FILE_INDEX]); 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]);
} }

View File

@@ -1,7 +1,6 @@
Name: ascenddockerplugin Name: ascenddockerplugin
Version: 1.0.0 Version: 1.0.0
Release: 1 Release: 1
BuildArch: noarch
Summary: simple RPM package Summary: simple RPM package
License: FIXME License: FIXME
@@ -29,7 +28,7 @@ BINDIR=/usr/local/bin
if [ ! -d "${DIR}" ]; then if [ ! -d "${DIR}" ]; then
mkdir ${DIR} mkdir ${DIR}
fi fi
${BINDIR}/ascend-docker-plugin-install-helper ${DST} ${SRC} ${BINDIR}/ascend-docker-plugin-install-helper add ${DST} ${SRC}
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
echo "create damon.json failed\n" echo "create damon.json failed\n"
exit 1 exit 1
@@ -38,6 +37,18 @@ fi
echo "create damom.json success\n" echo "create damom.json success\n"
%preun %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 %postun