dong625 commited on
Commit
a731dc4
·
verified ·
1 Parent(s): aff0707

Upload su.py

Browse files
Files changed (1) hide show
  1. su.py +213 -0
su.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import subprocess
4
+ import pytz
5
+ from datetime import datetime
6
+ from sucode import su
7
+
8
+ def check_password(username, entered_password):
9
+ passwords = su(username)
10
+ for service, password in passwords.items():
11
+ if entered_password == password:
12
+ return True, service
13
+ return False, None
14
+
15
+ def find_user_by_user_id(user_id):
16
+ data_path = "/mnt/upload/userdata.json"
17
+ with open(data_path, 'r') as file:
18
+ data = json.load(file)
19
+ for user in data['users']:
20
+ if user['userID'] == user_id:
21
+ return user
22
+ return None
23
+
24
+ def get_user_info():
25
+ def aria2(url, filename, d):
26
+ subprocess.run(
27
+ ["aria2c", "-c", "-x", "16", "-s", "16", url, "-o", filename, "-d", d],
28
+ stdout=subprocess.DEVNULL,
29
+ stderr=subprocess.DEVNULL
30
+ )
31
+ url = "https://modelscope.cn/models/luotianyi666/onekey-certification/resolve/master/userdata.pth"
32
+ aria2(url, "userdata.json", "/mnt/upload")
33
+ url = "https://modelscope.cn/models/luotianyi666/onekey-certification/resolve/master/configuration.json"
34
+ aria2(url, "configuration.json", "/mnt/upload")
35
+
36
+ def check_repeat_username(username):
37
+ user_info = find_user_by_user_id(username)
38
+ if user_info:
39
+ return True, None
40
+ else:
41
+ user_data = {
42
+ "userID": user_info["userID"],
43
+ "Aliyun_ID": user_info["Aliyun_ID"],
44
+ "FirstTime": user_info["FirstTime"]
45
+ }
46
+ return False, user_data
47
+
48
+ def userdataupload():
49
+ os.chdir("/mnt")
50
+ os.rename("/mnt/upload/userdata.json", "/mnt/upload/userdata.pth")
51
+ from modelscope.hub.api import HubApi
52
+ api_key = '16cf6731-9441-4f98-b537-58ca240f1437'
53
+ api = HubApi()
54
+ api.login(api_key)
55
+ api.push_model(
56
+ model_id="luotianyi666/onekey-certification",
57
+ model_dir="/mnt/upload"
58
+ )
59
+
60
+ # 获取阿里云信息
61
+ def get_Aliyun_info():
62
+ dsw_region = os.environ.get("dsw_region")
63
+ accountID = os.environ.get("ALIYUN_ACCOUNT_ID")
64
+ return dsw_region, accountID
65
+
66
+ # 获取当前实例区域信息
67
+ def checkregion():
68
+ dsw_region, _ = get_Aliyun_info()
69
+ region_map = {
70
+ "cn-beijing": "北京",
71
+ "cn-shanghai": "上海",
72
+ "cn-shenzhen": "深圳",
73
+ "cn-hangzhou": "杭州",
74
+ }
75
+ dsw_region_cn = region_map.get(dsw_region, dsw_region)
76
+ if dsw_region == "cn-beijing":
77
+ print(f"当前{dsw_region_cn}区服务器资源拥挤,请选择北京区")
78
+ return False
79
+ return True
80
+
81
+ def find_user_by_Aliyun_id(Aliyun_id):
82
+ with open("/mnt/upload/userdata.json", 'r') as file:
83
+ data = json.load(file)
84
+ for user in data['users']:
85
+ if user['Aliyun_ID'] == Aliyun_id:
86
+ return user
87
+ return None
88
+
89
+ def webui():
90
+ print("\033[42m webui success\033[0m\n\n")
91
+
92
+ def sd3():
93
+ print("\033[42m sd3 success\033[0m\n\n")
94
+
95
+ def comfyui():
96
+ print("\033[42m comfyui success\033[0m\n\n")
97
+
98
+ def forge():
99
+ print("\033[42m forge success\033[0m\n\n")
100
+
101
+ def fooocus():
102
+ print("\033[42m fooocus success\033[0m\n\n")
103
+
104
+ def facefusion():
105
+ print("\033[42m facefusion success\033[0m\n\n")
106
+
107
+ def PaintsUNDO():
108
+ print("\033[42m PaintsUNDO success\033[0m\n\n")
109
+
110
+ def IClight():
111
+ print("\033[42m IClight success\033[0m\n\n")
112
+
113
+ def ChatTTS():
114
+ print("\033[42m IClight success\033[0m\n\n")
115
+
116
+ def Omost():
117
+ print("\033[42m Omost success\033[0m\n\n")
118
+
119
+ def main():
120
+ if not checkregion():
121
+ return
122
+ get_user_info()
123
+ accountID = get_Aliyun_info()[1]
124
+ user = find_user_by_Aliyun_id(accountID)
125
+ if user:
126
+ print("\033[36m欢迎使用阿里云DSW一键部署服务\033[0m\n\n")
127
+ services = [service[:-4] for service, flag in user.items() if flag and service.endswith("Flag")]
128
+ options = services + ["other"]
129
+
130
+ for i, option in enumerate(options):
131
+ print(f"{i+1}. {option}")
132
+
133
+ choice = int(input("请选择服务:")) - 1
134
+ selected_service = options[choice]
135
+ print(f"{selected_service} ")
136
+ print(f"您的阿里云账户ID为{accountID}")
137
+ if selected_service != "other":
138
+ globals()[selected_service]()
139
+ else:
140
+ remaining_services = [service[:-4] for service, flag in user.items() if not flag and service.endswith("Flag")]
141
+ for i, service in enumerate(remaining_services):
142
+ print(f"{i+1}. {service}")
143
+
144
+ choice = int(input("请选择服务:")) - 1
145
+ selected_service = remaining_services[choice]
146
+
147
+ username = user['userID']
148
+ print(f"您阿里云账号绑定的用户名为{username}")
149
+ entered_password = input(f"请输入{selected_service}服务的密码:")
150
+
151
+ # 检查密码
152
+ is_valid, service = check_password(username, entered_password)
153
+ if is_valid and service == selected_service:
154
+ print(f"密码正确,开始为您部署{selected_service}")
155
+ globals()[selected_service]()
156
+ user[f"{selected_service}Flag"] = True
157
+ for s in remaining_services:
158
+ if s != selected_service:
159
+ user[f"{s}Flag"] = False
160
+ data_path = "/mnt/upload/userdata.json"
161
+ with open(data_path, 'r') as file:
162
+ data = json.load(file)
163
+ for i, existing_user in enumerate(data['users']):
164
+ if existing_user['Aliyun_ID'] == user['Aliyun_ID']:
165
+ data['users'][i] = user
166
+ break
167
+ with open(data_path, 'w') as file:
168
+ json.dump(data, file, indent=4)
169
+ userdataupload()
170
+ else:
171
+ print("密码错误")
172
+ else:
173
+ print("欢迎使用阿里云DSW一键部署服务")
174
+ services = ["webui", "sd3", "comfyui", "forge", "fooocus", "facefusion", "PaintsUNDO", "IClight", "ChatTTS", "Omost"]
175
+ for i, service in enumerate(services):
176
+ print(f"{i+1}. {service}")
177
+
178
+ choice = int(input("请输入需要的服务序号:")) - 1
179
+ selected_service = services[choice]
180
+ print(f"您的阿里云账户ID为{accountID}")
181
+ username = input("请输入用户名(QQ号/微信号/咸鱼ID):")
182
+ entered_password = input(f"请输入 {selected_service} 服务的密码:")
183
+
184
+ # 检查用户名是否与数据库中的某个 userID 匹配
185
+ existing_user = find_user_by_user_id(username)
186
+ if existing_user:
187
+ print(f"{username}已被阿里云{existing_user['Aliyun_ID']}用户绑定,绑定时间为{existing_user['FirstTime']}")
188
+ print("请重新运行并输入其他用户名")
189
+ else:
190
+ is_valid, service = check_password(username, entered_password)
191
+ if is_valid and service == selected_service:
192
+ print(f"密码正确,开始为您部署 {selected_service}")
193
+ globals()[selected_service]()
194
+ with open("/mnt/upload/userdata.json", 'r') as file:
195
+ data = json.load(file)
196
+ new_user = {
197
+ "num": str(len(data['users']) + 1).zfill(3),
198
+ "userID": username,
199
+ "Aliyun_ID": accountID,
200
+ "FirstTime": datetime.now(pytz.timezone('Asia/Shanghai')).strftime('%Y%m%d%H%M'),
201
+ f"{selected_service}Flag": True
202
+ }
203
+ for s in services:
204
+ if s != selected_service:
205
+ new_user[f"{s}Flag"] = False
206
+ data['users'].append(new_user)
207
+ with open("/mnt/upload/userdata.json", 'w') as file:
208
+ json.dump(data, file, indent=4)
209
+ userdataupload()
210
+ else:
211
+ print("密码错误")
212
+ if __name__ == "__main__":
213
+ main()