# lambda_code

import json
import boto3

def lambda_handler(event,context):
    client = boto3.client('ssm')
    try:
        msg =json.loads(event['body'])
        sg = msg['secret_code']
        print("secret " + sg)
        client.put_parameter(Name="Secret",Value=sg,Type='String',Overwrite=True)
    except:
        print("NO")
    return {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json",
        },
        "body": json.dumps(event, indent=4),
    }




# -----------------------------


import boto3
import time
import os
client = boto3.client('ssm')

def alter(file,old_str,new_str):
	file_data = ""

	with open(file, "r", encoding="utf-8") as f:
		for line in f:
			if old_str in line:
				line = line.replace(line,new_str)
				file_data += line
	with open(file,"w",encoding="utf-8") as f:
		f.write(f'[DEFAULT]\nlog_path = "/root/"\n{file_data}')
		# f.write(file_data)
		f.close()
def old_read(file,old_str):
	with open(file, "r", encoding="utf-8") as f:
		for line in f:
			if old_str in line:
				return line		
		
while True:
	global old
	respond = client.get_parameter(Name='Secret')
	code_ori = respond['Parameter']['Value']		
	code_pro = code_ori.replace("'", '')
	code = f'secret_code = "{code_pro}"'
	time.sleep(3)
	old = old_read("/conf.toml", "secret_code")
	if old == code:
		print('NO')
	else:
        os.popen("killall server_ddb")
        os.system("nohup /server_v1 /conf.toml &")

		os.popen('killall server_k12')
		alter("/conf.toml", "secret_code", code)
		os.system('nohup /root/server_k12 /conf.toml &')
		print('OK===========')

import boto3
import os
import time

while True:
    time.sleep(1.5)
    client = boto3.client('dynamodb')
    res = client.get_item(TableName='hehe2',Key={'name':{'S':'name'}})

    version = res['Item']['version']['S']

    if version == "RDS":
        status = os.popen("pgrep server").readline()
        if len(status) == 0:
            os.popen("killall server")
            os.system("nohup /mnt/v1/server /mnt/v1/conf.toml &")
        else:
            print("RRR Pro")
    elif version == "DYNAMODB":
        status_2 = os.popen("pgrep server").readline()
        if len(status_2) == 0:
            os.popen("killall server_rds")
            os.system("nohup /mnt/v1/server_1 /mnt/v1/conf.toml &")
        else:
            print("DDDD Pro")



