Jump to content

Calling PHP modules that access Processwire from a Bash script - Connection Refused error


FrancisChung
 Share

Recommended Posts

Hi,

I'm trying to write a bash script that automates our release procedures.

1 of the things I've written is a php module that clears the Procache settings and resets.

The php module runs if I call it from a browser but if I call it from a bash script I get a connection refused error.

Error: 	Exception: SQLSTATE[HY000] [2002] Connection refused (in /Users/FrancisChung/Sites/Develop/wire/core/ProcessWire.php line 293)

This error message was shown because: you are using the command line API Error has been logged. 

Assuming this is a security precaution, I was wondering if there is a different way around this?
Perhaps write a container php module that  launches the bash scripts instead?

Link to comment
Share on other sites

Have you tried to login a specific user from within the script? Maybe a specialy created user 'cli', who has superuser rights granted?

I'm not sure, but if you run that script form the browser, and you was logged in before with your admin account, than the script also runs in that account.

  • Like 2
Link to comment
Share on other sites

2 hours ago, horst said:

Have you tried to login a specific user from within the script? Maybe a specialy created user 'cli', who has superuser rights granted?

I'm not sure, but if you run that script form the browser, and you was logged in before with your admin account, than the script also runs in that account.

Hi @horst,

How do you login to PW via the API? I don't see a login method of any kind on the API Cheatsheet?

Never mind, I just saw the login method in the Session object

Link to comment
Share on other sites

// example
// PW3, bootstrap check user-account
	$PWuser = empty($_GET['user']) ? '' : $_GET['user'];
	$PWpass = empty($_GET['pass']) ? '' : $_GET['pass'];
	if(!$wire->get('user')->isLoggedIn() || !$wire->get('user')->hasRole('superuser|chef')) {
		$u = $wire->get('session')->login($PWuser, $PWpass);
		if($u) {
			$wire->get('user') = $u;
		}
		if(!$wire->get('user')->isLoggedIn() && !$wire->get('user')->hasRole('superuser|chef')) {
			echo "\n\nERROR: Incorrect User-Login!\n\nScript stops now!\n";
			exit(1);
		}
		echo "\nSuccessful logged in as: " . $wire->get('user')->name ."\n";
	}
	else {
		echo "\nAllready logged in as: " . $wire->get('user')->name ."\n";
	}

 

  • Like 3
Link to comment
Share on other sites

@FrancisChung, uhhm, I changed wire-> to $wire-> on the fly for the example to be more future ready with PW3 and possible multi instances.

So, please, can you test if it works with this too:

            if($u) {
                $wire('user')->user = $u;
            }

(see the preceding $, to invoke the instances aware $wire, and not the (static) main wire instance)

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...