def help_admin(request): context = { "wiki_url" : settings.CENTRAL_WIKI_URL, "ips": get_ip_addresses(include_loopback=False), "port": request.META.get("SERVER_PORT") or settings.USER_FACING_PORT(), } return context
def start_server(self, port=None): """ Start the server, for real (not to test) (cron and web server) Assumes the web server is shut down. """ self._print_message("Starting the server") # Start the server to validate manage_py_dir = os.path.join(self.dest_dir, 'kalite') # shift to an existing directory first to remove the reference to a deleted directory os.chdir(tempfile.gettempdir()) # now go back to the working directory os.chdir(manage_py_dir) stdout, stderr, exit_code, proc = call_outside_command_with_output( 'kaserve', wait=False, manage_py_dir=manage_py_dir, output_to_stdin=True, ) self._wait_for_server_to_be_up() self._print_message("Server should be accessible @ port %s.\n" % (port or settings.USER_FACING_PORT()))
def _wait_for_server_to_be_down(self): self._print_message("Waiting for server to go down") while True: try: requests.get('http://localhost:%s' % settings.USER_FACING_PORT()) time.sleep(1) except requests.exceptions.ConnectionError: break self._print_message("Server is down!")
def _wait_for_server_to_be_up(self): self._print_message("Waiting for the server to go up") while True: try: requests.get('http://localhost:%s' % settings.USER_FACING_PORT()) break except requests.exceptions.ConnectionError: time.sleep(1) self._print_message("Server is up!")
def handle(self, *args, **options): # Eliminate irrelevant settings for opt in BaseCommand.option_list: del options[opt.dest] # Parse the crappy way that runcherrypy takes args, # or the host/port for arg in args: if "=" in arg: (key, val) = arg.split("=") options[key] = val elif ":" in arg: (options["host"], options["port"]) = arg.split(":") elif isnumeric(arg): options["port"] = arg else: raise CommandError("Unexpected argument format: %s" % arg) # In order to avoid doing this twice when the autoreloader # loads this process again, only execute the initialization # code if autoreloader won't be run (daemonize), or if # RUN_MAIN is set (autoreloader has started) if options["daemonize"] or os.environ.get("RUN_MAIN"): self.setup_server_if_needed() # we do this on every server request, # as we don't know what happens when we're not looking. self.reinitialize_server() # In case any chronograph threads were interrupted the last time # the server was stopped, clear their is_running flags to allow # them to be started up again as needed. # PJK 20/03/2015 # Job.objects.update(is_running=False) call_command("collectstatic", interactive=False) # Now call the proper command if not options["production"]: call_command("runserver", "%s:%s" % (options["host"], options["port"])) else: del options["production"] sys.stdout.write( "To access KA Lite from another connected computer, try the following address(es):\n" ) for addr in get_ip_addresses(): sys.stdout.write("\thttp://%s:%s/\n" % (addr, settings.USER_FACING_PORT())) call_command( "runcherrypyserver", *["%s=%s" % (key, val) for key, val in options.items()])
def handle(self, *args, **options): if not options["interactive"]: options["username"] = options["username"] or getattr(settings, "INSTALL_ADMIN_USERNAME", None) or get_clean_default_username() options["hostname"] = options["hostname"] or get_host_name() sys.stdout.write(" \n") # blank allows ansible scripts to dump errors cleanly. sys.stdout.write(" _ __ ___ _ _ _ \n") sys.stdout.write(" | | / / / _ \ | | (_) | \n") sys.stdout.write(" | |/ / / /_\ \ | | _| |_ ___ \n") sys.stdout.write(" | \ | _ | | | | | __/ _ \ \n") sys.stdout.write(" | |\ \| | | | | |___| | || __/ \n") sys.stdout.write(" \_| \_/\_| |_/ \_____/_|\__\___| \n") sys.stdout.write(" \n") sys.stdout.write("http://kalite.learningequality.org\n") sys.stdout.write(" \n") sys.stdout.write(" version %s\n" % VERSION) sys.stdout.write(" \n") if sys.version_info >= (2,8) or sys.version_info < (2,6): raise CommandError("You must have Python version 2.6.x or 2.7.x installed. Your version is: %s\n" % sys.version_info) if options["interactive"]: sys.stdout.write("--------------------------------------------------------------------------------\n") sys.stdout.write("\n") sys.stdout.write("This script will configure the database and prepare it for use.\n") sys.stdout.write("\n") sys.stdout.write("--------------------------------------------------------------------------------\n") sys.stdout.write("\n") raw_input("Press [enter] to continue...") sys.stdout.write("\n") # Tried not to be os-specific, but ... hey. :-/ if not is_windows() and hasattr(os, "getuid") and os.getuid() == 502: sys.stdout.write("-------------------------------------------------------------------\n") sys.stdout.write("WARNING: You are installing KA-Lite as root user!\n") sys.stdout.write("\tInstalling as root may cause some permission problems while running\n") sys.stdout.write("\tas a normal user in the future.\n") sys.stdout.write("-------------------------------------------------------------------\n") if options["interactive"]: if not raw_input_yn("Do you wish to continue and install it as root?"): raise CommandError("Aborting script.\n") sys.stdout.write("\n") # Check to see if the current user is the owner of the install directory if not os.access(BASE_DIR, os.W_OK): raise CommandError("You do not have permission to write to this directory!") install_clean = not kalite.is_installed() database_kind = settings.DATABASES["default"]["ENGINE"] database_file = ("sqlite" in database_kind and settings.DATABASES["default"]["NAME"]) or None if database_file and os.path.exists(database_file): # We found an existing database file. By default, # we will upgrade it; users really need to work hard # to delete the file (but it's possible, which is nice). sys.stdout.write("-------------------------------------------------------------------\n") sys.stdout.write("WARNING: Database file already exists! \n") sys.stdout.write("-------------------------------------------------------------------\n") if not options["interactive"] \ or raw_input_yn("Keep database file and upgrade to KA Lite version %s? " % VERSION) \ or not raw_input_yn("Remove database file '%s' now? " % database_file) \ or not raw_input_yn("WARNING: all data will be lost! Are you sure? "): install_clean = False sys.stdout.write("Upgrading database to KA Lite version %s\n" % VERSION) else: install_clean = True sys.stdout.write("OK. We will run a clean install; \n") sys.stdout.write("the database file will be moved to a deletable location.\n") # After all, don't delete--just move. if not install_clean and not database_file and not kalite.is_installed(): # Make sure that, for non-sqlite installs, the database exists. raise Exception("For databases not using SQLIte, you must set up your database before running setup.") # Do all input at once, at the beginning if install_clean and options["interactive"]: if not options["username"] or not options["password"]: sys.stdout.write("\n") sys.stdout.write("Please choose a username and password for the admin account on this device.\n") sys.stdout.write("\tYou must remember this login information, as you will need\n") sys.stdout.write("\tto enter it to administer this installation of KA Lite.\n") sys.stdout.write("\n") (username, password) = get_username_password(options["username"], options["password"]) email = options["email"] (hostname, description) = get_hostname_and_description(options["hostname"], options["description"]) else: username = options["username"] or getattr(settings, "INSTALL_ADMIN_USERNAME", None) password = options["password"] or getattr(settings, "INSTALL_ADMIN_PASSWORD", None) email = options["email"] # default is non-empty hostname = options["hostname"] description = options["description"] if username and not validate_username(username): raise CommandError("Username must contain only letters, digits, and underscores, and start with a letter.\n") ######################## # Now do stuff ######################## # Move database file (if exists) if install_clean and database_file and os.path.exists(database_file): # This is an overwrite install; destroy the old db dest_file = tempfile.mkstemp()[1] sys.stdout.write("(Re)moving database file to temp location, starting clean install. Recovery location: %s\n" % dest_file) shutil.move(database_file, dest_file) # Should clean_pyc for (clean) reinstall purposes call_command("clean_pyc", interactive=False, verbosity=options.get("verbosity"), path=os.path.join(settings.PROJECT_PATH, "..")) # Migrate the database call_command("syncdb", interactive=False, verbosity=options.get("verbosity")) call_command("migrate", merge=True, verbosity=options.get("verbosity")) # Install data if install_clean: # Create device, load on any zone data call_command("generatekeys", verbosity=options.get("verbosity")) call_command("initdevice", hostname, description, verbosity=options.get("verbosity")) Facility.initialize_default_facility() #else: # Device exists; load data if required. # # Hackish, as this duplicates code from initdevice. # #if os.path.exists(InitCommand.data_json_file): # # This is a pathway to install zone-based data on a software upgrade. # sys.stdout.write("Loading zone data from '%s'\n" % InitCommand.data_json_file) # load_data_for_offline_install(in_file=InitCommand.data_json_file) # confirm_or_generate_zone() # Create the admin user if password: # blank password (non-interactive) means don't create a superuser admin = get_object_or_None(User, username=username) if not admin: call_command("createsuperuser", username=username, email=email, interactive=False, verbosity=options.get("verbosity")) admin = User.objects.get(username=username) admin.set_password(password) admin.save() # Now deploy the static files call_command("collectstatic", interactive=False) if not settings.CENTRAL_SERVER: # Move scripts for script_name in ["start", "stop", "run_command"]: script_file = script_name + system_script_extension() dest_dir = os.path.join(settings.PROJECT_PATH, "..") src_dir = os.path.join(dest_dir, "scripts") shutil.copyfile(os.path.join(src_dir, script_file), os.path.join(dest_dir, script_file)) shutil.copystat(os.path.join(src_dir, script_file), os.path.join(dest_dir, script_file)) start_script_path = os.path.realpath(os.path.join(settings.PROJECT_PATH, "..", "start%s" % system_script_extension())) # Run videoscan, on the distributed server. sys.stdout.write("Scanning for video files in the content directory (%s)\n" % settings.CONTENT_ROOT) call_command("videoscan") # done; notify the user. sys.stdout.write("\n") if install_clean: sys.stdout.write("CONGRATULATIONS! You've finished setting up the KA Lite server software.\n") sys.stdout.write("\tPlease run '%s' to start the server,\n" % start_script_path) sys.stdout.write("\tthen load one of the following addresses in your browser to complete the configuration:\n") for ip in get_ip_addresses(): sys.stdout.write("\t\thttp://%s:%d/\n" % (ip, settings.USER_FACING_PORT())) else: sys.stdout.write("CONGRATULATIONS! You've finished updating the KA Lite server software.\n") sys.stdout.write("\tPlease run '%s' to start the server.\n" % start_script_path) sys.stdout.write("\n")