@next-core/brick-container 2.92.26 → 2.93.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,260 @@
1
+ # -*- coding: utf-8 -*-
2
+ import json
3
+ import os
4
+ import traceback
5
+
6
+ import yaml
7
+ import sys
8
+ import errno
9
+ import shutil
10
+
11
+ reload(sys)
12
+ sys.setdefaultencoding('utf-8')
13
+
14
+ # 公共文件夹名
15
+ _APPLICATIONS_SA_FOLDER = "applications_sa"
16
+ _BRICKS_FOLDER = "bricks"
17
+ _TEMPLATES_FOLDER = "templates"
18
+ _CORE_FOLDER = "core"
19
+ _BRICK_NEXT_FOLDER = "brick_next"
20
+ _MICRO_APPS_FOLDER = "micro-apps"
21
+
22
+ # 公共路径
23
+ _INSTALL_BASE_PATH = "/usr/local/easyops"
24
+ _DEPENDENCIES_LOCK_BASE_PATH = os.path.join(
25
+ _INSTALL_BASE_PATH, _APPLICATIONS_SA_FOLDER, "dependencies_lock")
26
+
27
+
28
+ def init_dependencies_lock_dir():
29
+ # 初始化 /usr/local/easyops/applications_sa/dependencies_lock
30
+ if not os.path.exists(_DEPENDENCIES_LOCK_BASE_PATH):
31
+ try:
32
+ os.makedirs(_DEPENDENCIES_LOCK_BASE_PATH)
33
+ except OSError, e:
34
+ if e.errno != errno.EEXIST:
35
+ raise
36
+ print u"dependencies lock dir: {} already exist".format(_DEPENDENCIES_LOCK_BASE_PATH)
37
+
38
+ # 初始化子目录 bricks、templates、core、micro_apps
39
+ bricks_path = os.path.join(_DEPENDENCIES_LOCK_BASE_PATH, _BRICKS_FOLDER)
40
+ templates_path = os.path.join(
41
+ _DEPENDENCIES_LOCK_BASE_PATH, _TEMPLATES_FOLDER)
42
+
43
+ core_path = os.path.join(_DEPENDENCIES_LOCK_BASE_PATH, _CORE_FOLDER)
44
+ micro_apps_path = os.path.join(
45
+ _DEPENDENCIES_LOCK_BASE_PATH, _MICRO_APPS_FOLDER)
46
+
47
+ paths = [bricks_path, templates_path, core_path, micro_apps_path]
48
+ for path in paths:
49
+ if os.path.exists(path):
50
+ continue
51
+ try:
52
+ os.mkdir(path)
53
+ except OSError, e:
54
+ if e.errno != errno.EEXIST:
55
+ raise
56
+ print u"dependencies lock dir: {} already exist".format(path)
57
+ continue
58
+ print u"mkdir dependencies lock dir: {}".format(path)
59
+
60
+
61
+ def parse_plugin_name(plugin_name):
62
+ dependencies_lock_root_folder = ""
63
+ un_suffix_name = ""
64
+ if plugin_name.endswith("-NB"):
65
+ dependencies_lock_root_folder = _BRICKS_FOLDER
66
+ un_suffix_name = plugin_name.rstrip("-NB")
67
+ elif plugin_name.endswith("-NT"):
68
+ dependencies_lock_root_folder = _TEMPLATES_FOLDER
69
+ un_suffix_name = plugin_name.rstrip("-NT")
70
+
71
+ elif plugin_name == _BRICK_NEXT_FOLDER: # # brick_next -> core
72
+ dependencies_lock_root_folder = _CORE_FOLDER
73
+ # un_suffix_name = bricks_next_folder
74
+ # TODO: 其他
75
+
76
+ if un_suffix_name == "":
77
+ un_suffix_name = plugin_name
78
+ return un_suffix_name, dependencies_lock_root_folder
79
+
80
+
81
+ def build_dependencies_path_tree(dependencies):
82
+ dep_map = {}
83
+ for dependency in dependencies: # type: dict
84
+ name = dependency.get("name", "")
85
+ actual_version = dependency.get("actual_version", "")
86
+ if name == "" or actual_version == "":
87
+ continue
88
+ if name in dep_map:
89
+ continue
90
+
91
+ # 原路径 公共路径
92
+
93
+ un_suffix_name, dependency_type = parse_plugin_name(name)
94
+ if dependency_type == "":
95
+ dependency_type = name
96
+
97
+ # /usr/local/easyops/applications_sa/dependencies_lock templates general-list 1.30.0
98
+ link_path_base = os.path.join(
99
+ _DEPENDENCIES_LOCK_BASE_PATH, dependency_type, un_suffix_name, actual_version)
100
+ if un_suffix_name == _BRICK_NEXT_FOLDER:
101
+ link_path_base = os.path.join(
102
+ _DEPENDENCIES_LOCK_BASE_PATH, dependency_type, actual_version)
103
+
104
+ dependency = {}
105
+ # 直接在这里记录路径
106
+ # templates
107
+ dependency["dependency_type"] = dependency_type
108
+ # /usr/local/easyops/applications_sa/dependencies_lock/templates/general-list/1.30.0
109
+ dependency["link_path_base"] = link_path_base
110
+ # general-list
111
+ dependency["un_suffix_name"] = un_suffix_name
112
+ dep_map[name] = dependency
113
+
114
+ print "\n\n"
115
+
116
+ return dep_map
117
+
118
+
119
+ def read_dependencies_yaml(install_app_path):
120
+ dependencies_yaml_path = os.path.join(
121
+ install_app_path, "dependencies", "micro_app_dependencies.yaml")
122
+ print u"dependencies_yaml_path: {}".format(dependencies_yaml_path)
123
+ dependencies_fp = open(dependencies_yaml_path, "r")
124
+ data = yaml.load(dependencies_fp)
125
+ if not isinstance(data, dict):
126
+ raise u"micro_app_dependencies.yaml fmt error: {}".format(data)
127
+ return data.get("dependencies_lock", [])
128
+
129
+
130
+ def get_static_file_map(path):
131
+ file_map = {}
132
+ for root, _, files in os.walk(path, topdown=False):
133
+ if root not in file_map:
134
+ file_map[root] = []
135
+ if len(files) == 0:
136
+ continue
137
+ for filename in files:
138
+ file_path = os.path.join(root, filename)
139
+ if os.path.islink(file_path):
140
+ continue
141
+ file_map[root].append(filename)
142
+
143
+ return file_map
144
+
145
+
146
+ def read_union_apps_file(install_app_path):
147
+ union_app_file = os.path.join(install_app_path, "union-apps", "union-apps.json")
148
+ if not os.path.exists(union_app_file):
149
+ return []
150
+ with open(union_app_file, "r") as f:
151
+ return json.load(f)
152
+
153
+
154
+ def link_install_app_static_file(install_app_path):
155
+ # 读取union-apps.json文件
156
+ union_apps = read_union_apps_file(install_app_path)
157
+ for app in union_apps:
158
+ app_id = app["app_id"]
159
+ version = app["version"]
160
+ if app["use_brick_next_v3"]:
161
+ subdir_snippet = "v3"
162
+ else:
163
+ subdir_snippet = "v2"
164
+ current_app_path = os.path.join(install_app_path, _MICRO_APPS_FOLDER, subdir_snippet, app_id, version)
165
+ if not os.path.exists(current_app_path):
166
+ print u"current app path: {} not exist".format(current_app_path)
167
+ continue
168
+ current_app_path_public = os.path.join(_DEPENDENCIES_LOCK_BASE_PATH, _MICRO_APPS_FOLDER,
169
+ subdir_snippet, app_id, version)
170
+
171
+ # 联合打包中, micro-app会重复被打进包里(先删除文件, 保证目录文件是最新的数据)
172
+ if os.path.exists(current_app_path_public):
173
+ shutil.rmtree(current_app_path_public)
174
+ print u"delete old micr-app dir: {}".format(current_app_path_public)
175
+ os.makedirs(current_app_path_public)
176
+
177
+ print u"current app path: {}, current_app_path_public: {}".format(current_app_path, current_app_path_public)
178
+
179
+ static_file_map = get_static_file_map(current_app_path)
180
+ print u"---------------------------------------------"
181
+ for file_path_base, files in static_file_map.items():
182
+ _link_static_file(files, file_path_base,current_app_path, current_app_path_public)
183
+
184
+ print u"---------------------------------------------"
185
+ print u"\n\n"
186
+
187
+
188
+ # 硬链
189
+ def link_dependency_static_file(install_app_path):
190
+ dependency_apps = read_dependencies_yaml(install_app_path)
191
+ # 依赖的路径树
192
+ dependencies_path_tree = build_dependencies_path_tree(
193
+ dependency_apps) # type: dict
194
+ # 区分版本
195
+ for dependency_name, dependency_info in dependencies_path_tree.items():
196
+ dependency_type = dependency_info["dependency_type"]
197
+ un_suffix_name = dependency_info["un_suffix_name"]
198
+
199
+ # /usr/local/easyops/applications_sa/dependencies_lock/templates/general-list/1.30.0
200
+ dependency_dir_public = dependency_info["link_path_base"]
201
+ # /usr/local/easyops/applications_sa/easy-agile-standalone-NA/templates/general-list
202
+ dependency_dir_inside_base = os.path.join(install_app_path, dependency_type, un_suffix_name)
203
+
204
+ try:
205
+ static_file_map = get_static_file_map(dependency_dir_inside_base)
206
+ for file_path_base, files in static_file_map.items():
207
+ _link_static_file(files, file_path_base, dependency_dir_inside_base, dependency_dir_public)
208
+ except Exception as e:
209
+ raise RuntimeError("link file err: {}...".format(e))
210
+
211
+
212
+ def _link_static_file(files, file_path_base, dependency_dir_inside_base, dependency_dir_public):
213
+ for filename in files:
214
+ file_path = os.path.join(file_path_base, filename)
215
+ file_path_backup = file_path + ".back"
216
+
217
+ link_file_path = file_path.replace(
218
+ dependency_dir_inside_base, dependency_dir_public)
219
+ link_file_path_base = link_file_path.replace(filename, "")
220
+
221
+ try:
222
+ if not os.path.exists(link_file_path_base):
223
+ os.makedirs(link_file_path_base)
224
+ # 先备份文件
225
+ os.rename(file_path, file_path_backup)
226
+
227
+ # 如果文件不存在则移动文件
228
+ if not os.path.exists(link_file_path):
229
+ os.rename(file_path_backup, link_file_path)
230
+
231
+ # 创建硬链接
232
+ os.link(link_file_path, file_path)
233
+
234
+ except Exception as e:
235
+ exc_type, exc_value, exc_traceback = sys.exc_info()
236
+ traceback.print_exception(exc_type, exc_value, exc_traceback)
237
+ if os.path.exists(file_path_backup):
238
+ if os.path.exists(file_path):
239
+ os.remove(file_path)
240
+ os.rename(file_path_backup, file_path)
241
+ finally:
242
+ # 确保被删除
243
+ if os.path.exists(file_path_backup):
244
+ os.remove(file_path_backup)
245
+
246
+
247
+ if __name__ == "__main__":
248
+ if len(sys.argv) != 2:
249
+ print u"Usage: ./link_union_app_static_file.py $install_path"
250
+ sys.exit(1)
251
+ install_path = sys.argv[1]
252
+
253
+ # must init
254
+ init_dependencies_lock_dir()
255
+
256
+ # 硬链当前APP
257
+ link_install_app_static_file(install_path)
258
+
259
+ # 硬链依赖APP
260
+ link_dependency_static_file(install_path)
@@ -0,0 +1,148 @@
1
+ # -*- coding: utf-8 -*-
2
+ import json as simplejson
3
+ import sys
4
+ import os
5
+ import ens_api
6
+ import requests
7
+ import simplejson
8
+
9
+ reload(sys)
10
+ sys.setdefaultencoding("utf-8")
11
+
12
+ class NameServiceError(Exception):
13
+ pass
14
+
15
+
16
+ def join_host_port(host, port):
17
+ template = "%s:%s"
18
+ host_requires_bracketing = ':' in host or '%' in host
19
+ if host_requires_bracketing:
20
+ template = "[%s]:%s"
21
+ return template % (host, port)
22
+
23
+
24
+ session_id, ip, port = ens_api.get_service_by_name("", "logic.micro_app_service")
25
+ if session_id <= 0:
26
+ raise Exception("get name service logic.micro_app_service failed, no session_id")
27
+ MICRO_APP_ADDR = join_host_port(ip, port)
28
+
29
+ session_id, ip, port = ens_api.get_service_by_name("", "logic.micro_app_standalone_service")
30
+ if session_id <= 0:
31
+ raise NameServiceError("get nameservice logic.micro_app_standalone error, session_id={}".format(session_id))
32
+ MICRO_APP_SA_ADDR = join_host_port(ip, port)
33
+
34
+
35
+ def get_version(install_path):
36
+ # 开发环境没有version.ini文件,直接返回0.0.0
37
+ if not os.path.exists(os.path.join(install_path, "version.ini")):
38
+ return "0.0.0"
39
+ with open(os.path.join(install_path, "version.ini")) as f:
40
+ lines = f.readlines()
41
+ return lines[-1].strip()
42
+
43
+
44
+ def collect_app_info(app_path, report_app_id, version):
45
+ if not os.path.exists(app_path):
46
+ raise Exception("could not find app path {}".format(app_path))
47
+ bootstrap_file_name = ""
48
+ for f in os.listdir(app_path):
49
+ if f.startswith("bootstrap-mini.") and f.endswith(".json"):
50
+ bootstrap_file_name = f
51
+ if bootstrap_file_name is "":
52
+ raise Exception("bootstrap-mini.*.json not found in dir {}".format(app_path))
53
+ bootstrap_file = os.path.join(app_path, bootstrap_file_name)
54
+ print u"report app: {}, bootstrap_file: {}".format(report_app_id, bootstrap_file)
55
+ with open(bootstrap_file) as f:
56
+ bootstrap_content = f.read()
57
+ bootstrap_content_json = simplejson.loads(bootstrap_content)
58
+ # 跳过没有app字段的storyboard
59
+ if not bootstrap_content_json.get("storyboards"):
60
+ return None
61
+ storyboards = bootstrap_content_json.get("storyboards")
62
+ for story_board in storyboards:
63
+ if story_board["app"]["id"] == report_app_id:
64
+ app = {
65
+ "appId": story_board["app"]["id"],
66
+ "name": story_board["app"]["name"],
67
+ "internal": "true" if story_board["app"].get("internal") else "false",
68
+ "version": version,
69
+ "homepage": story_board["app"]["homepage"],
70
+ "status": "enabled", # 新安装状态默认是enabled的
71
+ "setActiveVersion": True,
72
+ "meta": simplejson.dumps(story_board["meta"], ensure_ascii=False),
73
+ "defaultConfig": story_board["app"].get("defaultConfig"),
74
+ "defaultContainer": story_board["app"].get("defaultContainer"),
75
+ "icons": story_board["app"].get("icons", {}),
76
+ "menuIcon": story_board["app"].get("menuIcon", {}),
77
+ "locales": story_board["app"].get("locales", {}),
78
+ "description": story_board["app"].get("description"),
79
+ "author": story_board["app"].get("author"),
80
+ }
81
+ return app
82
+
83
+
84
+ def report(org, app):
85
+ try:
86
+ create_or_update_micro_app_sa(org, app)
87
+ except NameServiceError, e:
88
+ raise e
89
+ except requests.HTTPError, e:
90
+ raise e
91
+
92
+
93
+ def create_or_update_micro_app_sa(org, app):
94
+ headers = {"org": str(org), "user": "defaultUser"}
95
+ url = "http://{}/api/v1/micro_app_standalone/report".format(MICRO_APP_SA_ADDR)
96
+ rsp = requests.post(url, json=app, headers=headers)
97
+ rsp.raise_for_status()
98
+ print "report app end"
99
+
100
+
101
+ def import_micro_app_permissions(org, permission_path):
102
+ if not os.path.exists(permission_path):
103
+ print "could not find permission path {}, will not import permissions".format(permission_path)
104
+ return
105
+ headers = {"org": str(org), "user": "defaultUser"}
106
+ url = "http://{}/api/micro_app/v1/permission/import".format(MICRO_APP_ADDR)
107
+
108
+ print "permission path is {}, will start import permissions".format(permission_path)
109
+ with open(permission_path) as f:
110
+ p_f_content = f.read()
111
+ permission_list = simplejson.loads(p_f_content)
112
+ body = {"permissionList": permission_list}
113
+ rsp = requests.post(url, json=body, headers=headers)
114
+ rsp.raise_for_status()
115
+
116
+ def read_union_apps_file(install_app_path):
117
+ union_app_file = os.path.join(install_app_path, "union-apps", "union-apps.json")
118
+ if not os.path.exists(union_app_file):
119
+ return []
120
+ with open(union_app_file, "r") as f:
121
+ return simplejson.load(f)
122
+
123
+
124
+ def report_union_apps(org, install_app_path):
125
+ union_apps = read_union_apps_file(install_path)
126
+ for app in union_apps:
127
+ app_id = app["app_id"]
128
+ version = app["version"]
129
+ if app["use_brick_next_v3"]:
130
+ subdir_snippet = "v3"
131
+ else:
132
+ subdir_snippet = "v2"
133
+ union_app_path = os.path.join(install_app_path, "micro-apps", subdir_snippet, app_id, version)
134
+ print u"report app: {}, current app path: {}".format(app_id, union_app_path)
135
+ app = collect_app_info(union_app_path, app_id, version)
136
+ if app:
137
+ report(org, app)
138
+ permission_file_path = os.path.join(union_app_path, "permissions", "permissions.json")
139
+ import_micro_app_permissions(org, permission_file_path)
140
+
141
+
142
+ if __name__ == "__main__":
143
+ if len(sys.argv) != 3:
144
+ print "Usage: ./report_union_micro_app.py $org $install_path"
145
+ sys.exit(1)
146
+ org = sys.argv[1]
147
+ install_path = sys.argv[2]
148
+ report_union_apps(org, install_path)