forked from LGinC/portainer-stack-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
125 lines (110 loc) · 4.19 KB
/
entrypoint.sh
File metadata and controls
125 lines (110 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
set -e
# set default endpointid=1
if [ -z "$INPUT_ENDPOINTID" ]; then
INPUT_ENDPOINTID=1
fi
if [ -z "$INPUT_DOCKER_COMPOSE" ]; then
echo 'compose is null'
compose=''
else
compose=$(echo "$INPUT_DOCKER_COMPOSE" | sed 's#\"#\\"#g' | sed ":a;N;s/\\n/\\\\n/g;ta") # replace charactor "->\" \n -> \\n
fi
#把stack name转为小写
stack=$(echo "$INPUT_STACKNAME" | tr "[:upper:]" "[:lower:]") #ToLowerCase
#请求/api/auth 进行身份验证 获取token
echo "get token : $INPUT_SERVERURL/api/auth"
Token_Result=$(curl -k --location --request POST ''$INPUT_SERVERURL'/api/auth' \
--data-raw '{"Username":"'$INPUT_USERNAME'", "Password":"'$INPUT_PASSWORD'"}')
# Token_Result = {"jwt":"xxxxxxxx"}
#todo: get token failed exit 1
token=$(echo "$Token_Result" | jq -r '.jwt')
if [ "$token" = "null" ]; then
echo 'Authorization failed'
echo "$Token_Result"
exit 1
fi
#pull image
#拉取镜像
echo "pull image: $INPUT_IMAGENAME"
base64Registry=$(printf '{"registryId":%s}' "$INPUT_REGISTRY_ID" | base64 -w0)
curl -k --location --request POST ''${INPUT_SERVERURL}'/api/endpoints/'$INPUT_ENDPOINTID'/docker/images/create?fromImage='$INPUT_IMAGENAME'' \
-H "Authorization: Bearer $token" -H "X-Registry-Auth: $base64Registry"
#get stacks
echo
echo "get stacks : $INPUT_SERVERURL/api/stacks"
#请求/api/stacks 查询stack列表
stacks=$(curl -k --location --request GET ''${INPUT_SERVERURL}'/api/stacks' \
--header 'Authorization: Bearer '$token'')
echo "stacks: $stacks"
#获取stack列表长度,如果为空则长度为0
length=$(echo "$stacks" | jq '.|length')
echo "length: $length"
#如果长度大于0
if [ $length -gt 0 ]; then
#查找同名stack
stackId=$(echo "$stacks" | jq '.[] | select(.Name=="'$stack'") | .Id') #find the stack name of INPUT_STACKNAME
echo "stackId: $stackId"
if [[ ! -z $stackId && $stackId -gt 0 ]]; then
if [ -z "$compose" ]; then
#find the current compose file content
#/api/stacks/${stackId}/file
echo "get stack file : $INPUT_SERVERURL/api/stacks/$stackId/file"
file_result=$(curl -k --location --request GET ''${INPUT_SERVERURL}'/api/stacks/'${stackId}/file'' \
--header 'Authorization: Bearer '$token'')
file_msg=$(echo "$file_result" | jq -r '.message')
if [ "$file_msg" != "null" ]; then
echo "get stack file failed"
echo "result: $file_result"
exit 1
fi
echo "file: $file_result"
compose=$(echo "$file_result" | jq '.StackFileContent')
update_content="{\"id\":${stackId},\"StackFileContent\":${compose},\"Env\":[]}"
else
update_content="{\"id\":${stackId},\"StackFileContent\":\"${compose}\",\"Env\":[]}"
fi
echo
echo "update stack id=$stackId"
#找到同名stack,更新stack
update_result=$(curl -k --location --request PUT ''${INPUT_SERVERURL}'/api/stacks/'${stackId}?endpointId=${INPUT_ENDPOINTID}'' \
--header 'Authorization: Bearer '$token'' \
--header 'Content-Type: application/json' \
--data-raw "$update_content")
update_result_msg=$(echo "$update_result" | jq -r '.message')
if [ "$update_result_msg" != "null" ]; then
echo "update stack failed"
echo "body: $update_content"
echo "result: $update_result"
exit 1
fi
echo "update success"
exit 0
fi
fi
#create stacks
#创建stack
echo 'cannot find the same name stack'
echo 'create stack : '${INPUT_SERVERURL}'/api/stacks?endpointId='$INPUT_ENDPOINTID'&method=string&type=2'
if [ -z "$compose" ]; then
echo "docker_compose can't be empty for create stack"
exit 1
fi
echo
echo "{\"Name\":\"'${INPUT_STACKNAME}'\",\"StackFileContent\":\"${compose}\",\"Env\":[]}"
#输出结果
create_content="{\"Name\":\"'${stack}'\",\"pullImage\": true,\"StackFileContent\":\"${compose}\",\"Env\":[]}"
result=$(curl -k --location --request POST \
"${INPUT_SERVERURL}/api/stacks/create/standalone/string?endpointId=${INPUT_ENDPOINTID}" \
--header 'Authorization: Bearer '${token}'' \
--header "Content-Type: application/json" \
--data-raw "$create_content")
echo "$result"
echo
#如果结果中message不为空则说明有异常
message=$(echo $result | jq -r '.message')
if [ "$message" != "null" ]; then
echo "create failed: $message"
exit 1
fi
exit 0