@@ -52,7 +52,6 @@ def get_vm_ip():
5252
5353def create_visit_ip_text (ip ):
5454 return "Hurray! your scraper is up and running. Visit http://{}/ to use it." .format (ip )
55-
5655def wait_till_up (ip ):
5756 """
5857 Polls the given IP address every 10 seconds for 180 seconds to check if it's up.
@@ -63,11 +62,11 @@ def wait_till_up(ip):
6362 Raises:
6463 Exception: If the IP is not up after 180 seconds.
6564 """
66- timeout = 180 # Total time to wait in seconds
65+ timeout = 60 # Total time to wait in seconds
6766 interval = 1 # Time to wait between checks in seconds
68- elapsed_time = 0
67+ end_time = time . time () + timeout
6968
70- while elapsed_time <= timeout :
69+ while time . time () < end_time :
7170 try :
7271 # Attempt to connect to the IP address
7372 response = requests .get (f"http://{ ip } /api" , timeout = 5 )
@@ -80,7 +79,6 @@ def wait_till_up(ip):
8079 pass
8180
8281 time .sleep (interval )
83- elapsed_time += interval
8482
8583 # If the function hasn't returned after the loop, raise an exception
8684 raise Exception (f"The VM at http://{ ip } / is not up after { timeout } seconds. Please check the logs using "
@@ -479,6 +477,7 @@ def install_scraper_in_vm(git_repo_url, folder_name, max_retry):
479477 install_scraper (git_repo_url , folder_name , max_retry )
480478 click .echo ("Successfully installed the Scraper." )
481479 # todo check status is it running or error?
480+
482481def get_filename_from_url (url ):
483482 from urllib .parse import urlparse
484483 return os .path .basename (urlparse (url ).path .rstrip ("/" ))
@@ -490,8 +489,11 @@ def install_desktop_app_in_vm(
490489 api_path_prefix
491490 ):
492491 # Validate api_path_prefix
493- api_path_prefix = api_path_prefix if api_path_prefix else ""
494- if api_path_prefix :
492+ api_path_prefix = os .path .normpath (api_path_prefix ) if api_path_prefix else ""
493+
494+ if api_path_prefix == "." :
495+ api_path_prefix = ""
496+ elif api_path_prefix :
495497 if not api_path_prefix .startswith ("/" ):
496498 api_path_prefix = "/" + api_path_prefix
497499 if api_path_prefix .endswith ("/" ):
@@ -506,8 +508,7 @@ def install_desktop_app_in_vm(
506508 default_name = get_filename_from_url (debian_installer_url )
507509
508510 delete_installer (default_name )
509- wget_command = f"wget { debian_installer_url } "
510- subprocess .run (wget_command , shell = True , check = True , stderr = subprocess .STDOUT )
511+ subprocess .run (["wget" , debian_installer_url ], check = True , stderr = subprocess .STDOUT )
511512 install_command = f"sudo apt --fix-broken install ./{ default_name } -y"
512513 subprocess .run (install_command , shell = True , check = True , stderr = subprocess .STDOUT )
513514 package_name = subprocess .check_output (f"dpkg-deb -f ./{ default_name } Package" , shell = True ).decode ().strip ()
@@ -531,7 +532,8 @@ def install_desktop_app_in_vm(
531532 package_service_name = f"{ package_name } .service"
532533 package_service_content = f"""[Unit]
533534Description={ package_name } Service
534- After=network.target
535+ After=network.target { xvfb_service_name }
536+ Requires={ xvfb_service_name }
535537StartLimitInterval=0
536538[Service]
537539Type=simple
@@ -565,7 +567,7 @@ def install_desktop_app_in_vm(
565567 click .echo ("Now, Checking API Status..." )
566568 ip = get_vm_ip ()
567569 wait_till_desktop_api_up (ip , api_path_prefix )
568- click .echo (f"Hurray! your desktop app is up and running. Visit http://{ ip } { api_path_prefix } / to see the API Docs." )
570+ click .echo (f"Hurray! your desktop app is up and running. Visit http://{ ip } { api_path_prefix or "/" } to see the API Docs." )
569571
570572def delete_installer (default_name ):
571573 if os .path .exists (default_name ):
@@ -577,8 +579,8 @@ def setup_apache_load_balancer_desktop_app(port, api_path_prefix):
577579 DocumentRoot /var/www/html
578580 ErrorLog ${{APACHE_LOG_DIR}}/error.log
579581 CustomLog ${{APACHE_LOG_DIR}}/access.log combined
580- ProxyPass { api_path_prefix } / http://127.0.0.1:{ port } { api_path_prefix } /
581- ProxyPassReverse { api_path_prefix } / http://127.0.0.1:{ port } { api_path_prefix } /
582+ ProxyPass { api_path_prefix or "/" } http://127.0.0.1:{ port } { api_path_prefix or "/" }
583+ ProxyPassReverse { api_path_prefix or "/" } http://127.0.0.1:{ port } { api_path_prefix or "/" }
582584</VirtualHost>"""
583585 write_file_sudo (apache_conf , "/etc/apache2/sites-available/000-default.conf" )
584586
@@ -587,7 +589,12 @@ def validate_url(url):
587589 response = requests .head (url , allow_redirects = True , timeout = 20 )
588590 response .raise_for_status ()
589591 except requests .exceptions .RequestException as e :
590- raise Exception (f"URL validation failed: { e } " )
592+ # Retry with GET if HEAD fails (some servers don't support HEAD)
593+ try :
594+ response = requests .get (url , allow_redirects = True , timeout = 20 )
595+ response .raise_for_status ()
596+ except requests .exceptions .RequestException as e2 :
597+ raise Exception (f"The URL { url } does not point to a valid Debian installer." )
591598
592599def wait_till_desktop_api_up (ip , api_path_prefix ):
593600 """
@@ -600,14 +607,17 @@ def wait_till_desktop_api_up(ip, api_path_prefix):
600607 Raises:
601608 Exception: If the IP is not up after 180 seconds.
602609 """
603- timeout = 180 # Total time to wait in seconds
610+ timeout = 60 # Total time to wait in seconds
604611 interval = 1 # Time to wait between checks in seconds
605- elapsed_time = 0
612+ end_time = time . time () + timeout
606613
607- while elapsed_time <= timeout :
614+ while time . time () < end_time :
608615 try :
609616 # Attempt to connect to the IP address
610- response = requests .get (f"http://{ ip } { api_path_prefix } /ui/app-props" , timeout = 5 )
617+ if api_path_prefix :
618+ response = requests .get (f"http://{ ip } { api_path_prefix } /ui/app-props" , timeout = 5 )
619+ else :
620+ response = requests .get (f"http://{ ip } /ui/app-props" , timeout = 5 )
611621
612622 # If the response is successful, return without raising an exception
613623 if response .status_code == 200 :
@@ -617,10 +627,10 @@ def wait_till_desktop_api_up(ip, api_path_prefix):
617627 pass
618628
619629 time .sleep (interval )
620- elapsed_time += interval
621-
622630 # If the function hasn't returned after the loop, raise an exception
623- raise Exception (f"The Desktop App at http://{ ip } / is not up after { timeout } seconds." )
631+ raise Exception (
632+ f"The Desktop Api at http://{ ip } / is not up after { timeout } seconds. Are you sure you have enabled the Api in api-config.ts?"
633+ )
624634
625635# python -m bota.vm
626636if __name__ == "__main__" :
0 commit comments