Server Toggle

Do you run your own server? If so, this script makes toggling various servers (Apache, MySQL, Sendmail, or WebMin) on and off a little easier.

OS version: OS X

property admin_password : ""
property mysql_password : ""
property mysql_user : ""
property mysql_host : ""
property last_choice : ""

--load the properties
repeat while admin_password = ""
	set admin_password to my cl_ask_password("Enter the Root Password:")
end repeat

repeat while mysql_password = ""
	set mysql_password to my cl_ask_password("Enter the MySQL Password:")
end repeat

repeat while mysql_user = ""
	set mysql_user to my cl_ask_password("Enter the MySQL User:")
end repeat

repeat while mysql_host = ""
	set mysql_host to my cl_ask_password("Enter the MySQL Host:")
end repeat

set the_actions to {"Apache: Start", "Apache: Stop", "Apache: Graceful Restart", "", "MySQL: Start", "MySQL: Stop", "", "Sendmail: Restart", "", "Webmin: Start", "Webmin: Stop"}
set the_scripts to {"apachectl start", "apachectl stop", "apachectl graceful", "", "/usr/local/bin/mysqld --user=mysql &", "/usr/local/bin/mysqladmin -u " & mysql_user & " -h " & mysql_host & " -p" & mysql_password & " shutdown", "", "/System/Library/StartupItems/Sendmail/Sendmail restart", "", "/etc/webmin/start", "/etc/webmin/stop"}

if last_choice = "" then set last_choice to item 1 of the_actions
	set the_choice to (choose from list the_actions with prompt "What would you like to do?" default items {last_choice}) as string
	if the_choice = "false" then
	else if the_choice ? "" then
		set last_choice to the_choice
		exit repeat
	end if
end repeat
set the_script to my match_paired_lists(the_choice, the_actions, the_scripts, "")

	set the_response to ""
	ignoring application responses
		set the_response to do shell script the_script password admin_password with administrator privileges
	end ignoring
	if the_response = "" then set the_response to the_choice & "ed."
	display dialog the_response buttons {"OK"} default button 1 with icon 1 giving up after 10
on error the_error
	display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 10
end try


on match_paired_lists(match_value, list_a, list_b, no_match_val)
	repeat with i from 1 to count of list_a
		if (item i of list_a) = match_value then return (item i of list_b)
	end repeat
	return no_match_val
end match_paired_lists

on cl_ask_password(the_prompt)
	if the_prompt = "" then set the_propmt to "Administrator Password:"
	return text returned of (display dialog the_prompt default answer "" buttons {"Cancel", "OK"} default button 2 with icon 1)
end cl_ask_password