feat: add init project to Makefile
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,6 +1,8 @@
 | 
			
		||||
venv/
 | 
			
		||||
node_modules/
 | 
			
		||||
 | 
			
		||||
init.lock
 | 
			
		||||
 | 
			
		||||
.idea/
 | 
			
		||||
.vscode/
 | 
			
		||||
*.swp
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										9
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								Makefile
									
									
									
									
									
								
							@@ -37,5 +37,14 @@ revision:
 | 
			
		||||
	cd $(API_APPLICATION_NAME)/db && \
 | 
			
		||||
	PYTHONPATH='../..' ALEMBIC_MIGRATIONS=True alembic revision --autogenerate
 | 
			
		||||
 | 
			
		||||
venv-api:
 | 
			
		||||
	cd api && \
 | 
			
		||||
	poetry install
 | 
			
		||||
 | 
			
		||||
install:
 | 
			
		||||
	make migrate head && \
 | 
			
		||||
	cd api && \
 | 
			
		||||
	poetry run python3 api/utils/init.py
 | 
			
		||||
 | 
			
		||||
%::
 | 
			
		||||
	echo $(MESSAGE)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										60
									
								
								api/api/utils/init.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								api/api/utils/init.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
import os
 | 
			
		||||
import asyncio
 | 
			
		||||
import hashlib
 | 
			
		||||
import secrets
 | 
			
		||||
 | 
			
		||||
from api.db.connection.session import get_connection
 | 
			
		||||
from api.db.tables.account import account_table, account_keyring_table, AccountRole, KeyType, KeyStatus
 | 
			
		||||
 | 
			
		||||
INIT_LOCK_FILE = "../init.lock"
 | 
			
		||||
DEFAULT_LOGIN = "vorkout"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def hash_password(password: str) -> str:
 | 
			
		||||
    return hashlib.sha256(password.encode()).hexdigest()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def generate_password() -> str:
 | 
			
		||||
    return secrets.token_urlsafe(20)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def init():
 | 
			
		||||
    if os.path.exists(INIT_LOCK_FILE):
 | 
			
		||||
        print("Sorry, service is already initialized")
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    async with get_connection() as conn:
 | 
			
		||||
        password = generate_password()
 | 
			
		||||
        hashed_password = hash_password(password)
 | 
			
		||||
 | 
			
		||||
        create_user_query = account_table.insert().values(
 | 
			
		||||
            name=DEFAULT_LOGIN,
 | 
			
		||||
            login=DEFAULT_LOGIN,
 | 
			
		||||
            role=AccountRole.OWNER,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        res = await conn.execute(create_user_query)
 | 
			
		||||
        user_id = res.lastrowid
 | 
			
		||||
 | 
			
		||||
        create_key_query = account_keyring_table.insert().values(
 | 
			
		||||
            owner_id=user_id,
 | 
			
		||||
            key_type=KeyType.PASSWORD,
 | 
			
		||||
            key_value=hashed_password,
 | 
			
		||||
            status=KeyStatus.ACTIVE,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        await conn.execute(create_key_query)
 | 
			
		||||
 | 
			
		||||
        await conn.commit()
 | 
			
		||||
 | 
			
		||||
        await conn.close()
 | 
			
		||||
 | 
			
		||||
    with open(INIT_LOCK_FILE, "w") as lock_file:
 | 
			
		||||
        lock_file.write("initialized\n")
 | 
			
		||||
 | 
			
		||||
    print(f"Login: {DEFAULT_LOGIN}")
 | 
			
		||||
    print(f"Password: {password}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    asyncio.get_event_loop().run_until_complete(init())
 | 
			
		||||
		Reference in New Issue
	
	Block a user