-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_stack_portainer.sh
140 lines (124 loc) · 3.79 KB
/
create_stack_portainer.sh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# test1, test2 or test3
PROJECT=$1
# gitcompose, gitswarm, or compose
MODE=$2
BASE_URL="http://localhost:9000"
getstackid()
{
idresult=$(curl "${BASE_URL}/api/stacks" \
-H "Authorization: Bearer ${jwtToken}" \
-H "accept: application/json" \
-H "Content-Type:application/json" | jq ".[] | select(.Name==\"${PROJECT}snapshot\") | .Id" )
echo $idresult
}
retrievetoken()
{
jwtToken=$(curl -s -X POST -d "{ \"username\" : \"admin\", \"password\" : \"admin123\" }" "${BASE_URL}/api/auth" --header "Content-Type:application/json" | jq -r '.jwt' )
}
createstackgitcompose()
{
echo "Creating stack from GIT with name ${PROJECT} and git URL ${GIT_URL}"
curl -X POST "${BASE_URL}/api/stacks?type=2&method=repository&endpointId=1" \
-H "Authorization: Bearer ${jwtToken}" \
-H "accept: application/json" \
-H "Content-Type:application/json" \
--data-raw "
{
\"Name\": \"${PROJECT}snapshot\",
\"RepositoryURL\": \"${GIT_URL}\",
\"RepositoryReferenceName\": \"refs/heads/master\",
\"ComposeFilePathInRepository\": \"compose/${PROJECT}/snapshot/docker-compose.yml\",
\"RepositoryAuthentication\": false,
\"Env\": []
}
"
}
createstackgitswarm()
{
echo "Creating stack from GIT with name ${PROJECT} and git URL ${GIT_URL}"
curl -X POST "${BASE_URL}/api/stacks?type=1&method=repository&endpointId=1" \
-H "Authorization: Bearer ${jwtToken}" \
-H "accept: application/json" \
-H "Content-Type:application/json" \
--data-raw "
{
\"Name\": \"${PROJECT}snapshot\",
\"SwarmID\": \"lriv6yox3s5ea6i28ryd85u5r\",
\"RepositoryURL\": \"${GIT_URL}\",
\"RepositoryReferenceName\": \"refs/heads/master\",
\"ComposeFilePathInRepository\": \"swarm/${PROJECT}/snapshot/docker-compose.yml\",
\"RepositoryAuthentication\": false,
\"Env\": []
}
"
}
createstackcompose()
{
echo "Creating stack from COMPOSE with name ${PROJECT}"
curl -X POST "${BASE_URL}/api/stacks?type=2&method=file&endpointId=1" \
-H "Authorization: Bearer ${jwtToken}" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "Name=${PROJECT}snapshot" \
-F "EndpointID=1" \
-F "[email protected]"
}
deletestack()
{
local deleteid=$1
echo "Deleting project $PROJECT with id $deleteid"
curl -X DELETE "${BASE_URL}/api/stacks/$deleteid" \
-H "Authorization: Bearer ${jwtToken}" \
-H "accept: application/json" \
-H "Content-Type:application/json"
}
isvalidid()
{
local testid=$1
re='^[0-9]+$'
if ! [[ $testid =~ $re ]] ; then
echo "Testing string $testid for being valid ID"
echo "error: Something went wrong determining the project ID" >&2; exit 1
fi
}
isemptyid()
{
local testid=$1
if [ -n "$testid" ]; then
echo "Expected no stackid but got $testid"
exit 1
fi
}
# MAIN
# delete docker network
docker network rm "${PROJECT}snapshot_network"
docker network rm "${PROJECT}snapshot_network_compose"
# create docker network
docker network create -d overlay "${PROJECT}snapshot_network"
docker network create --attachable "${PROJECT}snapshot_network_compose"
PROJECT_NR=${PROJECT: -1}
GIT_URL="https://github.com/enrico2828/test-portainer${PROJECT_NR}.git"
for i in {1..50}
do
echo "###### BATCH RUN $i ########"
retrievetoken
isemptyid $stackid
if [ "$MODE" == "gitcompose" ]; then
createstackgitcompose
elif [ "$MODE" == "gitswarm" ]; then
createstackgitswarm
elif [ "$MODE" == "compose" ]; then
createstackcompose
else
echo "Mode must be \'gitcompose, \'gitswarm\' or \'compose\', exiting"
exit 1
fi
stackid=$(getstackid)
isvalidid "$stackid"
deletestack "$stackid"
stackid=$(getstackid)
isemptyid "$stackid"
done
# delete docker network
docker network rm "${PROJECT}snapshot_network"
docker network rm "${PROJECT}snapshot_network_compose"