Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/gae/index.py

241 lines
6.3 KiB

6 years ago
# coding: utf-8
import time
import random
import os
import json
import re
import sys
sys.path.append(os.getcwd() + "/class/core")
import mw
6 years ago
6 years ago
def getPluginName():
return 'gae'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
6 years ago
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
6 years ago
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
6 years ago
def status():
return 'start'
6 years ago
def getAllProjectList(search):
path = mw.getWwwDir()
6 years ago
dlist = []
if os.path.exists(path):
for filename in os.listdir(path):
tmp = {}
filePath = path + '/' + filename
if os.path.isdir(filePath):
if search == '':
tmp['name'] = filename
tmp['dir'] = filePath
dlist.append(tmp)
else:
if filename.find(search) != -1:
tmp['name'] = filename
tmp['dir'] = filePath
dlist.append(tmp)
return dlist
6 years ago
def checkProjectListIsSet(data):
dlen = len(data)
for x in range(dlen):
6 years ago
path = getServerDir() + '/' + data[x]['name'] + '.json'
6 years ago
if os.path.exists(path):
6 years ago
if os.path.getsize(path) == 0:
data[x]['isset'] = False
else:
data[x]['isset'] = True
6 years ago
else:
data[x]['isset'] = False
return data
6 years ago
def projectListEdit():
args = getArgs()
if not 'name' in args:
return 'missing name!'
file = getServerDir() + '/' + args['name'] + '.json'
if not os.path.exists(file):
mw.execShell('touch ' + file)
6 years ago
return file
def projectListDel():
args = getArgs()
if not 'name' in args:
return 'missing name!'
file = getServerDir() + '/' + args['name'] + '.json'
if os.path.exists(file):
content = mw.readFile(file)
6 years ago
contentObj = json.loads(content)
asyncUser = contentObj['client_email']
cmd = getServerDir() + '/google-cloud-sdk/bin/'
mw.execShell(cmd + 'gcloud auth revoke ' + asyncUser)
mw.execShell('rm -rf ' + file)
6 years ago
return 'ok'
6 years ago
def checkUserExist(cmd, user):
data = mw.execShell(cmd + 'gcloud auth list | grep ' + user)
6 years ago
if data[0] == '':
return False
return True
6 years ago
def projectListAsync():
import subprocess
args = getArgs()
if not 'name' in args:
return 'missing name!'
file = getServerDir() + '/' + args['name'] + '.json'
if not os.path.exists(file):
6 years ago
return 'not configured file!'
6 years ago
content = mw.readFile(file)
6 years ago
contentObj = json.loads(content)
asyncUser = contentObj['client_email']
cmd = getServerDir() + '/google-cloud-sdk/bin/'
projectDir = mw.getWwwDir() + '/' + args['name']
6 years ago
6 years ago
if not checkUserExist(cmd, asyncUser):
mw.execShell(
6 years ago
cmd + 'gcloud auth activate-service-account --key-file ' + file)
pName = contentObj['project_id']
setUserCmd = cmd + 'gcloud config set account ' + asyncUser
setUserCmd += ' && ' + cmd + 'gcloud config set project ' + pName
asyncCmd = setUserCmd + ' && cd ' + projectDir + \
' && ' + cmd + 'gcloud app deploy << y'
6 years ago
6 years ago
taskAdd = (None, 'gae[async]',
'execshell', '0', time.strftime('%Y-%m-%d %H:%M:%S'), asyncCmd)
mw.M('tasks').add('id,name,type,status,addtime, execstr', taskAdd)
6 years ago
return 'ok'
6 years ago
def projectListCmd():
args = getArgs()
if not 'name' in args:
return 'missing name!'
file = getServerDir() + '/' + args['name'] + '.json'
if not os.path.exists(file):
return 'not configured file!'
content = mw.readFile(file)
6 years ago
contentObj = json.loads(content)
asyncUser = contentObj['client_email']
cmd = getServerDir() + '/google-cloud-sdk/bin/'
6 years ago
pName = contentObj['project_id']
projectDir = mw.getWwwDir() + '/' + args['name']
6 years ago
6 years ago
setUserCmd = 'sudo ' + cmd + 'gcloud config set account ' + asyncUser
setUserCmd += ' && sudo ' + cmd + 'gcloud config set project ' + pName
asyncCmd = setUserCmd + ' && sudo cd ' + projectDir + \
' && sudo ' + cmd + 'gcloud app deploy <<y'
6 years ago
return asyncCmd
def projectListUrl():
args = getArgs()
if not 'name' in args:
return 'missing name!'
file = getServerDir() + '/' + args['name'] + '.json'
if not os.path.exists(file):
return 'not configured file!'
content = mw.readFile(file)
6 years ago
contentObj = json.loads(content)
asyncUser = contentObj['client_email']
plist = asyncUser.split('@')
url = 'https://' + plist[0] + '.appspot.com'
return url
6 years ago
def projectList():
args = getArgs()
page = 1
page_size = 10
search = ''
if 'page' in args:
page = int(args['page'])
if 'page_size' in args:
page_size = int(args['page_size'])
if 'search' in args:
search = args['search']
dlist = getAllProjectList(search)
dlist_sum = len(dlist)
start = (page - 1) * page_size
ret_data = dlist[start:start + page_size]
6 years ago
ret_data = checkProjectListIsSet(ret_data)
6 years ago
data = {}
data['data'] = ret_data
data['list'] = mw.getPage(
6 years ago
{'count': dlist_sum, 'p': page, 'row': 10, 'tojs': 'projectList'})
return mw.getJson(data)
6 years ago
6 years ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
4 years ago
print(status())
6 years ago
elif func == 'project_list':
4 years ago
print(projectList())
6 years ago
elif func == 'project_list_edit':
4 years ago
print(projectListEdit())
6 years ago
elif func == 'project_list_del':
4 years ago
print(projectListDel())
6 years ago
elif func == 'project_list_async':
4 years ago
print(projectListAsync())
6 years ago
elif func == 'project_list_cmd':
4 years ago
print(projectListCmd())
6 years ago
elif func == 'project_list_url':
4 years ago
print(projectListUrl())
6 years ago
else:
4 years ago
print('error')