import os
import signal
import subprocess

work_space = 'E:\\Workspace\\aurora'
work_space= os.getcwd()
pid_output = 'p_output'
print('work_space: ' + work_space)
print('开始运行……')


def kill_process(name):
    try:
        # iterating through each instance of the process
        for line in os.popen("ps ax | grep " + name + " | grep -v grep"):
            fields = line.split()
            # extracting Process ID from the output
            pid = fields[0]
            # terminating process
            os.kill(int(pid), signal.SIGKILL)
        print("Process {} Successfully terminated".format(name))
    except:
        print("Error Encountered while running script")


def clear_poutput(name):  # 定义函数名
    b = os.getcwd()
    filePath = os.path.join(b, name)  # 在当前py文件所在路径下的new文件中创建txt
    with open(filePath, 'a+', encoding='utf-8') as f:
        f.truncate(0)


def poutput(name, pid):  # 定义函数名
    b = os.getcwd()
    filePath = os.path.join(b, name)  # 在当前py文件所在路径下的new文件中创建txt
    with open(filePath, 'a+') as f:
        f.write(pid+'\n')  # 加\n换行显示


# dotnet Aurora.Gateway.dll --urls "http://*:6002"
# dotnet build E:\Workspace\aurora\Aurora.Gateway\Aurora.Gateway.csproj ;cd E:\Workspace\aurora\Aurora.Gateway\bin\Debug\net5.0;dotnet Aurora.Gateway.dll

def start_dotnetservice(name, runPath, port,env='Development'):
    print('启动 ' + name)
    projectPath = os.path.join(runPath, name)
    buildPath=os.path.join('bin','Debug','net5.0')
    dllPath = os.path.join(projectPath, buildPath)
    subprocess.run('dotnet build', shell=True, cwd=projectPath)
    ret = subprocess.Popen('dotnet {}.dll --urls "http://*:{}" --environment {} '.format(name,
                                                                       port,env), shell=True, cwd=dllPath)
    poutput(pid_output, "service {},pid {}".format(name, str(ret.pid)))


def start_service(name, cmd):
    print('启动 ' + name)
    print('run -> ' + cmd)
    # ret = subprocess.Popen(cmd)
    ret = subprocess.Popen(cmd, shell=True)
    # 记录下 pid
    poutput(pid_output, "service {},pid {}".format(name, str(ret.pid)))


clear_poutput(pid_output)

print('启动 Consul')
start_service('Consul', 'consul agent -dev')

# cmd 查看端口占用 netstat -aon|findstr "8500"
# cmd 杀死进程 taskkill /f /pid 18xx8

# Gateway
s_name = 'Aurora.Gateway'
runPath = os.path.join(work_space)
start_dotnetservice(s_name, runPath, 5000)

# Tenant
s_name = 'Aurora.Tenant.Api'
runPath = os.path.join(work_space, 'Aurora.Microservices',
                       'Aurora.Tenant')
start_dotnetservice(s_name, runPath, 6000)

# Auth
s_name = 'Aurora.Auth'
runPath = os.path.join(work_space)
start_dotnetservice(s_name, runPath, 7000)

# Core.Api
s_name = 'Aurora.Core.Api'
runPath = os.path.join(work_space, 'Aurora.Microservices',
                       'Aurora.Core')
start_dotnetservice(s_name, runPath, 8000)

# WebHost
s_name = 'Aurora.WebHost'
runPath = os.path.join(work_space)
start_dotnetservice(s_name, runPath, 9000)

# HttpReports
s_name = 'Aurora.HttpReports'
runPath = os.path.join(work_space)
start_dotnetservice(s_name, runPath, 9100)

iStr = input('输入任意字符以结束……')
