o
    SDiJ                  
   @   sP  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZmZ d dlZd dlZd dlmZ d dlmZmZmZmZ d dlmZ d d	l m!Z! d d
l"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+ d dl,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2 e3e4Z5dede6fddZ7dede8fddZ9dedefddZ:G dd deZ;dee; fddZ<G dd deZ=de6d e6de=fd!d"Z>G d#d$ d$e6e	Z?G d%d& d&eZ@d'e6de@fd(d)ZAG d*d+ d+eZBd,e6d-eddfd.d/ZCd0e6dee= fd1d2ZDde6dee= fd3d4ZEd'e6d5ee6e6f ddfd6d7ZFd,e6dee6ddf fd8d9ZGg d:ZHg d;ZId<e!d=ede)fd>d?ZJd<e!d'e6d@e@ddfdAdBZKd<e!d'e6ddfdCdDZLG dEdF dFeZMdGeMd<e!ddfdHdIZNd<e!ddfdJdKZO		LdTde%eedf ejPdMdNf dOe%e8ej#dPdQdNf defdRdSZQdS )U    N)Enum)cycle)Path)AnyDict	GeneratorListOptionalUnion)Client)	BaseModelEmailStrTypeAdapterValidationError)Text)RichToolkit)Option)	Annotated)	APIClient)	AppConfigget_app_configwrite_app_config)is_logged_in)get_rich_toolkithandle_http_errors)"validate_environment_variable_namepathreturnc                 C   s   | j S N)namer    r!   Z/var/www/Datamplify/venv/lib/python3.10/site-packages/fastapi_cloud_cli/commands/deploy.py_get_app_name    s   r#   c                    s4   g d}t  fdd|D rdS  jdkrdS dS )N)z.venv__pycache__z.mypy_cachez.pytest_cachec                 3   s    | ]}| j v V  qd S r   )parts).0partr    r!   r"   	<genexpr>(   s    z(_should_exclude_entry.<locals>.<genexpr>Tz.pycF)anysuffix)r   parts_to_excluder!   r    r"   _should_exclude_entry%   s   
r,   c                 C   s   t d|  tj| td}t }t d| dt  }t	|| d }t d| d}t
|d }|D ]}| r?q8|j||| d	 |d
7 }q8W d    n1 sXw   Y  t d| |S )Nz&Starting archive creation for path: %s)should_exclude_entryzCreated temp directory: %szfastapi-cloud-deploy-z.tarzArchive will be created at: %sr   w)arcname   z*Archive created successfully with %s files)loggerdebugrignorewalkr,   tempfilemkdtempuuiduuid4r   tarfileopenis_diraddrelative_to)r   filestemp_dirr   tar_path
file_counttarfilenamer!   r!   r"   archive1   s$   
rD   c                   @   s&   e Zd ZU eed< eed< eed< dS )Teamidslugr   N__name__
__module____qualname__str__annotations__r!   r!   r!   r"   rE   I   s   
 rE   c                  C   sR   t  } | d}|  | d }W d    n1 sw   Y  dd |D S )Nz/teams/datac                 S      g | ]}t |qS r!   )rE   model_validater&   teamr!   r!   r"   
<listcomp>V       z_get_teams.<locals>.<listcomp>r   getraise_for_statusjson)clientresponserN   r!   r!   r"   
_get_teamsO   s   
r[   c                   @   s   e Zd ZU eed< eed< dS )AppResponserF   rG   NrH   r!   r!   r!   r"   r\   Y   s   
 r\   team_idapp_namec                 C   sT   t  }|jd|| dd}|  t| W  d    S 1 s#w   Y  d S )N/apps/)r   r]   rX   )r   postrW   r\   rP   rX   )r]   r^   rY   rZ   r!   r!   r"   _create_app^   s   $rb   c                   @   sB   e Zd ZdZdZdZdZdZdZdZ	dZ
ed	d d
efddZdS )DeploymentStatuswaiting_uploadready_for_buildbuilding
extractingbuilding_image	deployingsuccessfailedstatusr   c                 C   s8   | j d| jd| jd| jd| jd| jd| jd| jdi| S )	NzWaiting for uploadzReady for buildBuilding
ExtractingzBuilding image	DeployingSuccessFailed)rd   re   rf   rg   rh   ri   rj   rk   )clsrl   r!   r!   r"   to_human_readablet   s   	z"DeploymentStatus.to_human_readableN)rI   rJ   rK   rd   re   rf   rg   rh   ri   rj   rk   classmethodrL   rs   r!   r!   r!   r"   rc   j   s    rc   c                   @   s>   e Zd ZU eed< eed< eed< eed< eed< eed< dS )CreateDeploymentResponserF   app_idrG   rl   dashboard_urlurlN)rI   rJ   rK   rL   rM   rc   r!   r!   r!   r"   ru      s   
 ru   rv   c                 C   sR   t  }|d|  d}|  t| W  d    S 1 s"w   Y  d S )Nr_   /deployments/)r   ra   rW   ru   rP   rX   )rv   rY   rZ   r!   r!   r"   _create_deployment   s
   $rz   c                   @   s&   e Zd ZU eed< eeef ed< dS )RequestUploadResponserx   fieldsN)rI   rJ   rK   rL   rM   r   r!   r!   r!   r"   r{      s   
 r{   deployment_idarchive_pathc              	   C   s*  t d|  t d|| j t x}t \}t d |d|  d}|  t	|
 }t d|j t d |j|j|jd|d	id
}|  t d t d |d|  d}|  t d W d    n1 svw   Y  W d    d S W d    d S 1 sw   Y  d S )Nz-Starting deployment upload for deployment: %sz Archive path: %s, size: %s byteszRequesting upload URL from APIry   z/uploadzReceived upload URL: %szStarting file upload to S3filerb)rN   r>   z"File upload completed successfullyz%Notifying API that upload is completez/upload-completez%Upload notification sent successfully)r1   r2   statst_sizer   r   ra   rW   r{   rP   rX   rx   r|   r:   )r}   r~   fastapi_clientrY   rZ   upload_dataupload_responsenotify_responser!   r!   r"   _upload_deployment   s<   




Pr   app_slugc                 C   sl   t  &}|d|  }|jdkr	 W d    d S |  | }W d    n1 s,w   Y  t|S )Nr_   i  )r   rV   status_coderW   rX   r\   rP   )r   rY   rZ   rN   r!   r!   r"   _get_app   s   



r   c                 C   sZ   t  }|jdd| id}|  | d }W d    n1 s!w   Y  dd |D S )Nr_   r]   )paramsrN   c                 S   rO   r!   )r\   rP   r&   appr!   r!   r"   rS      rT   z_get_apps.<locals>.<listcomp>rU   )r]   rY   rZ   rN   r!   r!   r"   	_get_apps   s   r   env_varsc                 C   sJ   t  }|jd|  d|d}|  W d    d S 1 sw   Y  d S )Nr_   z/environment-variables/r`   )r   patchrW   )rv   r   rY   rZ   r!   r!   r"   _create_environment_variables   s   
"r   c              	   c   s    t  8}|jdd|  ddd}|  | E d H  W d    n1 s'w   Y  W d    d S W d    d S 1 s?w   Y  d S )NGETry   z/build-logs<   )timeout)r   streamrW   
iter_lines)r}   rY   rZ   r!   r!   r"   _stream_build_logs   s   "r   )
u+   🚀 Preparing for liftoff! Almost there...uA   👹 Sneaking past the dependency gremlins... Don't wake them up!u>   🤏 Squishing code into a tiny digital sandwich. Nom nom nom.u?   📉 Server space running low. Time to delete those cat videos?uM   🐢 Uploading at blazing speeds of 1 byte per hour. Patience, young padawan.uN   🔌 Connecting to server... Please stand by while we argue with the firewall.uQ   💥 Oops! We've angered the Python God. Sacrificing a rubber duck to appease it.u3   🧙 Sprinkling magic deployment dust. Abracadabra!uB   👀 Hoping that @tiangolo doesn't find out about this deployment.uF   🍪 Cookie monster detected on server. Deploying anti-cookie shields.)uS   😅 Well, that's embarrassing. We're still waiting for the deployment to finish...u9   🤔 Maybe we should have brought snacks for this wait...u   🥱 Yawn... Still waiting...uK   🤯 Time is relative... Especially when you're waiting for a deployment...toolkitpath_to_deployc           
   	   C   s  | j d| dddstd|   | d!}t|dd t }W d    n1 s.w   Y  W d    n1 s=w   Y  |   | jd	d
dd |D d}|   | j dddd}|   |s| d#}t|dd t|j	}W d    n1 sw   Y  W d    n1 sw   Y  |   |s| 
d td| jddd |D d}nF| jdt|d}|   | jdd+}t| t|j	|}W d    n1 sw   Y  |d|j  W d    n1 sw   Y  t|j	|j	d}	t||	 |	S )NzSetup and deploy [blue]z[/]?dirtagr   zFetching teams...z-Error fetching teams. Please try again later.)messagez&Select the team you want to deploy to:rR   c                 S      g | ]
}t |j|d qS )r   value)r   r   rQ   r!   r!   r"   rS         z"_configure_app.<locals>.<listcomp>)r   optionsz Do you want to create a new app?r   T)r   defaultzFetching apps...z,Error fetching apps. Please try again later.z=No apps found in this team. You can create a new app instead.r0   z%Select the app you want to deploy to:c                 S   r   r   )r   rG   r   r!   r!   r"   rS   ,  r   )r   zWhat's your app name?)titler   zCreating app...r   z$App created successfully! App slug: )rv   r]   )confirmtyperExit
print_lineprogressr   r[   askr   rF   printinputr#   rb   logrG   r   r   )
r   r   r   teamsrR   create_new_appappsr   r^   
app_configr!   r!   r"   _configure_app   sn   



r   
deploymentc           
   	   C   s  t t}| jddd |   | d|j d|j d |   d}t }t }| jt|dd	d
}t	|j
D ]}t | }t|}	d|	v rX|t|	d   |	ddkr|d |d|j d|j d |d |d|j d|j d  nA|	ddkr|d |d|j d|j d td|dkrt t}t | dkrt||_t }q;W d    d S W d    d S 1 sw   Y  d S )Nu+   Checking the status of your deployment 👀cloudr   z'You can also check the status at [link=][/link]g        T   )inline_logslines_to_showr   typecomplete u3   🐔 Ready the chicken! Your app is ready at [link=z)You can also check the app logs at [link=rk   u>   😔 Oh no! Something went wrong. Check out the logs at [link=r0         )r   WAITING_MESSAGESr   r   rw   time	monotonicr   nextr   rF   rX   loadsr   r   	from_ansirstriprV   rx   r   r   LONG_WAIT_MESSAGESr   )
r   rv   r   messagestime_elapsed
started_atlast_message_changed_atr   linerN   r!   r!   r"   _wait_for_deploymentC  s`   






"r   c              	   C   s   | j ddds	d S |   i }	 | jddd}| dkrnt|s(| d	 n| jd
dd}|||< |   q|   | d'}t| t|| W d    n1 sVw   Y  |	d W d    d S 1 skw   Y  d S )Nz+Do you want to setup environment variables?envr   Tz4Enter the environment variable name: [ENTER to skip]F)requiredr   z)[error]Invalid environment variable name.z%Enter the environment variable value:)passwordz#Setting up environment variables...z*Environment variables set up successfully!)
r   r   r   stripr   r   r   r   r   r   )r   rv   r   keyr   r   r!   r!   r"   _setup_environment_variables  s6   
"r   c                   @   s   e Zd ZU eed< dZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dZee ed< dZee ed< dZee ed	< dS )
SignupToWaitingListemailNr   organizationrole	team_sizelocationuse_casesecret_code)rI   rJ   rK   r   rM   r   r	   rL   r   r   r   r   r   r   r!   r!   r!   r"   r     s   
 r   resultc              
   C   s   | dD}t +}t| |jd| jddd}|  W d    n1 s(w   Y  W d    n1 s7w   Y  |d W d    d S 1 sLw   Y  d S )NzSending your request...z/users/waiting-listrX   )moder`   u9   Let's go! Thanks for your interest in FastAPI Cloud! 🚀)r   r   r   ra   
model_dumprW   r   )r   r   r   rY   rZ   r!   r!   r"   _send_waitlist_form  s   

"r   c                 C   s  ddl m} | jddd |   | jddttd}|   t|d	}| jd
ddr|   |d| j	d}|j
dddd |j
dddd |j
dddd |j
dddd |j
dddd |j
dddd |j
d d!d"d | }ztd#|i|}W n ty   | d$ Y d S w |   | jd%d&dr|   t||  tt tjd'd(gtjtjd)d* W d    n1 sw   Y  |   | d+ d S d S ),Nr   )FormzkWe're currently in private beta. If you want to be notified when we launch, please fill out the form below.waitlistr   zEnter your email:T)r   	validator)r   z?Do you want to get access faster by giving us more information?zWaitlist form)styler   NamezJohn Doe)labelplaceholderr   Organizationz	Acme Inc.rR   rE   zTeam Ar   Role	Developerr   LocationzSan Franciscor   z%How do you plan to use FastAPI Cloud?zI'm building a web appr   zSecret code123456r   z.[error]Invalid form data. Please try again.[/]zDo you agree to
- Terms of Service: [link=https://fastapicloud.com/legal/terms]https://fastapicloud.com/legal/terms[/link]
- Privacy Policy: [link=https://fastapicloud.com/legal/privacy-policy]https://fastapicloud.com/legal/privacy-policy[/link]
termsr:   zraycast://confettiF)stdoutstderrchecku2   Thank you for your interest in FastAPI Cloud! 🚀)rich_toolkit.formr   r   r   r   r   r   r   r   r   	add_inputrunrP   r   r   
contextlibsuppress	Exception
subprocessDEVNULL)r   r   r   r   formr!   r!   r"   _waitlist_form  s   
r   Fz:A path to the folder containing the app you want to deploy)help	skip_waitz	--no-waitz"Skip waiting for deployment statusc           	   
   C   s  t d t d| | t 4}t s"t d t| td|jddd |  | p2t	
 }t d| t|}|sYt d	 t||d
}|  t||j |  nt d |d |  |jddd9}t| t d|j t|j}W d   n1 sw   Y  |st d |d tdW d   n1 sw   Y  t d t| pt	
 }|jddA}t|( t d|j t|j}|d|j  |d t|j| W d   n1 sw   Y  |d W d   n	1 sw   Y  |  |s!t d t||j|d nt d |d|j d|j d  W d   dS W d   dS 1 sHw   Y  dS )!uB   
    Deploy a [bold]FastAPI[/bold] app to FastAPI Cloud. 🚀
    zDeploy command startedzDeploy path: %s, skip_wait: %sz)User not logged in, showing waitlist formr0   zStarting deploymentFastAPIr   zDeploying from path: %sz(No app config found, configuring new app)r   z5Existing app config found, proceeding with deploymentzDeploying app...zChecking app...T)	transientzChecking app with ID: %sNzApp not found in APIz>App not found. Make sure you're logged in the correct account.zCreating archive for deploymentzCreating deploymentr   zCreating deployment for app: %sz2Deployment created successfully! Deployment slug: zUploading deployment...z!Deployment uploaded successfully!z"Waiting for deployment to complete)r   z%Skipping deployment wait as requestedz-Check the status of your deployment at [link=r   r   )r1   r2   r   r   r   r   r   print_titler   r   cwdr   r   r   rv   r   r   r   r   	set_errorrD   rF   rz   r   rG   r   r   rw   )	r   r   r   r   r   r   r   r~   r   r!   r!   r"   deploy  sv   

















>$r  )NF)Rr   rX   loggingr   r9   r5   r   r7   enumr   	itertoolsr   pathlibr   typingr   r   r   r   r	   r
   r3   r   httpxr   pydanticr   r   r   r   	rich.textr   rich_toolkitr   rich_toolkit.menur   typing_extensionsr   fastapi_cloud_cli.utils.apir   fastapi_cloud_cli.utils.appsr   r   r   fastapi_cloud_cli.utils.authr   fastapi_cloud_cli.utils.clir   r   fastapi_cloud_cli.utils.envr   	getLoggerrI   r1   rL   r#   boolr,   rD   rE   r[   r\   rb   rc   ru   rz   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   Argumentr  r!   r!   r!   r"   <module>   s     

	)

E
?'
[

