-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (53 loc) · 2.04 KB
/
main.py
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
import requests
import argparse
import json
def main(args):
stack_id = ""
env = {}
git_reference_name = ""
git_username = ""
response = requests.get(args.url + "/api/stacks", headers={
"X-API-Key": args.apiKey
})
stacks = json.loads(response.content)
for stack in stacks:
if stack["Name"] == args.stackName and stack["EndpointId"] == args.endpointId:
stack_id = stack["Id"]
env = stack["Env"]
git_reference_name = stack["GitConfig"]["ReferenceName"]
git_username = stack["GitConfig"]["Authentication"]["Username"]
break
if stack_id == "":
raise SystemExit(f"Stack with name {args.stackName} not found.")
data = {
"env": env,
"prune": False,
"pullImage": True,
"repositoryAuthentication": True,
"repositoryReferenceName": git_reference_name,
"repositoryUsername": git_username,
}
if args.prune:
data["prune"] = True
print("Running redeploy...")
response = requests.put(
args.url + "/api/stacks/" + str(stack_id) + "/git/redeploy?endpointId=" + str(args.endpointId),
headers={
"X-API-Key": args.apiKey
},
json=data
)
if response.status_code > 200:
raise SystemExit(f"Redeploy was finished with error " + str(response.status_code) + ":\n" + str(response.content))
print("Success")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="Portainer stack redeploy tool",
description="Redeploys stack from git"
)
parser.add_argument("--url", type=str, required=True, help="Base Portainer's service URL")
parser.add_argument("--apiKey", type=str, required=True, help="Portainer API Key")
parser.add_argument("--endpointId", type=int, required=True, help="Environment ID")
parser.add_argument("--stackName", type=str, required=True, help="Stack name")
parser.add_argument("--prune", type=bool, required=False, help="Prune unused services")
main(parser.parse_args())