This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
publish_docs.py
executable file
·122 lines (86 loc) · 3.12 KB
/
publish_docs.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
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
#!/usr/bin/env python3
import sys
import getopt
import json
import requests
from pathlib import Path
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
def get_docs_farmer_url():
return 'https://farmer.storefrontcloud.io/instance/docs-europe-west1-gcp-storefrontcloud-io'
def get_docs_headers(user_id, api_key):
return {
'content-type': 'application/json',
'x-user-id': user_id,
'x-api-key': api_key
}
def merge_additionall_applications(current, to_merge):
merged = []
new_item = True
for additional_app in current:
if additional_app['name'] == to_merge['name']:
additional_app = to_merge
new_item = False
merged.append(additional_app)
if new_item == True:
merged.append(to_merge)
return merged
def get_additional_apps(user_id, api_key):
response = requests.get(get_docs_farmer_url(), headers=get_docs_headers(user_id, api_key))
instance = response.json()
if 'instance' in instance and 'additional_apps' in instance['instance'] and 'apps' in instance['instance']['additional_apps']:
return instance['instance']['additional_apps']['apps']
return False
def patch_additional_apps(user_id, api_key, payload):
response = requests.patch(get_docs_farmer_url(), data=json.dumps(payload), headers=get_docs_headers(user_id, api_key))
return response
def publis_docs(user_id, api_key, name, tag, image, path, port, has_base_path):
additional_apps = get_additional_apps(user_id, api_key)
if not additional_apps:
raise Exception("Additional application not configured")
to_merge = {
"name": name,
"tag": tag,
"image": image,
"path": path,
"port": port,
"has_base_path": str2bool(has_base_path)
}
merged_additionall_applications = merge_additionall_applications(additional_apps, to_merge)
payload = {'additional_apps': {'apps': merged_additionall_applications}}
response = patch_additional_apps(user_id, api_key, payload)
return response
def help():
return 'publish_docs.py -u user-id -a api-key -n name -t tag -i image -p path -r port -s has-base-path'
def main(argv):
"""Main function"""
try:
opts, args = getopt.getopt(argv,"hu:a:n:t:i:p:r:s:")
except getopt.GetoptError:
print(help())
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(help())
sys.exit()
else:
if opt in ("-u"):
user_id = arg
if opt in ("-a"):
api_key = arg
if opt in ("-n"):
name = arg
if opt in ("-t"):
tag = arg
if opt in ("-i"):
image = arg
if opt in ("-p"):
path = arg
if opt in ("-r"):
port = arg
if opt in ("-s"):
has_base_path = arg
response = publis_docs(user_id, api_key, name, tag, image, path, port, has_base_path)
print("::set-output name=response::" + str(response.status_code))
if __name__ == "__main__":
main(sys.argv[1:])