133 lines
3.8 KiB
Python
133 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import os.path
|
|
import requests
|
|
import time
|
|
import subprocess
|
|
import json
|
|
from hashlib import sha512
|
|
|
|
def hash_password(globalSalt, password):
|
|
globalSalt = globalSalt.encode('utf-8')
|
|
password = password.encode('utf-8')
|
|
return sha512(globalSalt+password+globalSalt).hexdigest()
|
|
|
|
env = os.environ
|
|
wwwRoot = '/var/www/html'
|
|
samRoot = '%s/sam'%wwwRoot
|
|
indexPHP = '%s/index.php'%wwwRoot
|
|
if os.path.exists('%s/hwe/d_setting/DB.php'):
|
|
exit(0)
|
|
|
|
useWaitIndex = False
|
|
if not os.path.exists(indexPHP):
|
|
useWaitIndex = True
|
|
fp = open(indexPHP, 'wt', encoding='utf-8')
|
|
fp.write("""
|
|
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<title>HIDCHE - Auto Install Process</title>
|
|
<meta http-equiv="refresh" content="5"/>
|
|
</head>
|
|
<body>
|
|
<h1>Please wait for installation.</h1>
|
|
</body>
|
|
</html>
|
|
""")
|
|
fp.close()
|
|
|
|
while True:
|
|
try:
|
|
r = requests.head('http://web/sam/f_install/j_install_status.php')
|
|
if r.status_code == 200:
|
|
break
|
|
except Exception:
|
|
pass
|
|
print("Waiting for web connection...", flush=True)
|
|
time.sleep(2)
|
|
|
|
while True:
|
|
result = subprocess.call('nc -z -v -w30 db 3306'.split(' '), stderr=subprocess.STDOUT)
|
|
if result == 0:
|
|
break
|
|
print("Waiting for database connection...", flush=True)
|
|
time.sleep(2)
|
|
|
|
session = requests.session()
|
|
|
|
db_root_pw = hash_password('root'+env['HIDCHE_PW_SALT'], env['MYSQL_USER']+env['MYSQL_PASSWORD'])[:32]
|
|
db_root_name = '%s_root'%env['HIDCHE_DB_PREFIX']
|
|
|
|
if 'HIDCHE_IMAGE_REQUEST_KEY' in env and len(env['HIDCHE_IMAGE_REQUEST_KEY'])>=16:
|
|
image_request_key = env['HIDCHE_IMAGE_REQUEST_KEY']
|
|
else:
|
|
image_request_key = os.urandom(16).hex()
|
|
|
|
setupDBResult = session.post('http://web/sam/f_install/j_setup_db.php', {
|
|
'db_host':'db',
|
|
'db_port':3306,
|
|
'db_id':db_root_name,
|
|
'db_pw':db_root_pw,
|
|
'db_name':db_root_name,
|
|
'serv_host':env['HIDCHE_GAME_PATH'],
|
|
'shared_icon_path':'%s/icons'%env['HIDCHE_IMAGE_PATH'],
|
|
'game_image_path':'%s/game'%env['HIDCHE_IMAGE_PATH'],
|
|
'image_request_key':image_request_key,
|
|
'kakao_rest_key':env['HIDCHE_KAKAO_REST_KEY'],
|
|
'kakao_admin_key':env['HIDCHE_KAKAO_ADMIN_KEY'],
|
|
})
|
|
setupDBJsonResult = json.loads(setupDBResult.text)
|
|
print(setupDBJsonResult, flush=True)
|
|
|
|
globalSalt = setupDBJsonResult['globalSalt']
|
|
|
|
hashPassword = hash_password(globalSalt, env['HIDCHE_PASSWORD'])
|
|
|
|
query = {
|
|
'username':env['HIDCHE_ADMIN'],
|
|
'password':hashPassword,
|
|
'nickname':'운영자'
|
|
}
|
|
setupAdminResult = session.post('http://web/sam/f_install/j_create_admin.php', query)
|
|
setupAdminJsonResult = json.loads(setupAdminResult.text)
|
|
print(setupAdminJsonResult, flush=True)
|
|
|
|
loginResult = session.post('http://web/sam/j_login.php', query)
|
|
print(loginResult.text, flush=True)
|
|
|
|
serverList = str(env['HIDCHE_SERVER_LIST']).split(',')
|
|
serverList.reverse()
|
|
|
|
currentBranch = 'devel'
|
|
for idx, serverName in enumerate(serverList):
|
|
if idx == 0:
|
|
target = currentBranch
|
|
else:
|
|
target = 'origin/'+currentBranch
|
|
updateResult = session.post('http://web/sam/j_updateServer.php', {
|
|
'server':serverName,
|
|
'target':target
|
|
})
|
|
print(serverName, updateResult.text, flush=True)
|
|
|
|
db_pw = hash_password(serverName+env['HIDCHE_PW_SALT'], env['MYSQL_USER']+env['MYSQL_PASSWORD'])[:32]
|
|
db_name = '%s_%s'%(env['HIDCHE_DB_PREFIX'], serverName)
|
|
resetResult = session.post('http://web/sam/%s/j_install_db.php'%serverName, {
|
|
'db_host':'db',
|
|
'db_port':3306,
|
|
'db_id':db_name,
|
|
'db_pw':db_pw,
|
|
'db_name':db_name,
|
|
})
|
|
print(serverName, resetResult.text, flush=True)
|
|
|
|
if useWaitIndex:
|
|
fp = open(indexPHP, 'wt', encoding='utf-8')
|
|
fp.write("<?php header('location:sam',TRUE,301);")
|
|
fp.close()
|
|
os.chown(indexPHP, 33, 33)
|
|
|
|
print('Welcome to HIDCHE', flush=True)
|