@next-core/brick-container 2.92.1 → 2.92.3

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