How to restart binding with in rules?

karaf
Tags: #<Tag:0x00007f1e5b25a8c0>
(Rich Koshak) #8

I think you can trigger a rule based on the online/offline state of a Thing. There is no way I know of to check the state of a Thing in a Rule though, only Items.

It hasn’t made it into the docs yet as far as I can tell but you can find the syntax here

0 Likes

(Christian) #9

Ok, thank you. Is this feature already available in stable release or do I need a current snapshot?

There are the following states for a thing:

  • UNINITIALIZED
  • INITIALIZING
  • UNKNOWN
  • ONLINE
  • OFFLINE
  • REMOVING
  • REMOVED

I’ll try to implement the following two rules triggering a switch.

rule "Check dssBridge OFFLINE"
when
    Thing 'digitalstrom:dssBridge:302ed89f43f0xxx' received update OFFLINE or
    Thing 'digitalstrom:dssBridge:302ed89f43f0xxx' received update UNINITIALIZED or
    Thing 'digitalstrom:dssBridge:302ed89f43f0xxx' received update UNKNOWN
then
    swi_dss_online.sendCommand(OFF)
end
rule "Check dssBridge ONLINE"
when
    Thing 'digitalstrom:dssBridge:302ed89f43f0xxx' received update ONLINE
then
    swi_dss_online.sendCommand(ON)
end

Does this make sense?

The final goal is to restart the binding via execute binding.

0 Likes

(Rich Koshak) #10

You probably need the SNAPSHOT but I don’t really know.

I don’t know if you will ever see an UNINITIALIZED event to trigger a Rule as I think that is a Thing’s default state. Beyond that what you have looks like it should work.

0 Likes

(Christian) #11

ok, thank you. It works with current stable release. I’ll try now to implement the exec binding.

1 Like

(Christian) #12

Hi,

I need some advise regarding restarting a binding from rules.
I have installed sshpass, and can run the command from from ssh terminal and restart binding without typing karaf password. I have created a python script and put it in script folder. I can run the script from ssh terminal
like: ./restartspotify.py, and the binding is restarting.

restartspotify.py:

sshpass -p 'habopen' ssh openhab@localhost -p8101 'bundle:restart org.openhab.binding.spotify'

BUT, i can not get my rule to execute the python script without error.

rule:

rule "Restart Spotify Binding"
	when 
	Thing "spotify:device:dd997e5e:1a729da3-76b9-4234-a426-1c445aa638a2" changed or
	Item Restart_Spotify received command ON
	
	then
		var status = ThingAction.getThingStatusInfo("spotify:device:dd997e5e:1a729da3-76b9-4234-a426-1c445aa638a2").getStatus()
		//logError("Debug", "AudioGruppe status | " + status)
		
		if (status.toString() == 'OFFLINE')
		{
			//logError("Debug", "Attempting to restart spotify binding")
			executeCommandLine("/etc/openhab2/scripts/restartspotify.py")
					
		}
		
	end

openhab.log

2017-09-16 19:54:17.434 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/etc/openhab2/scripts/restartspotify.py'
2017-09-16 19:54:19.090 [WARN ] [shd.server.session.ServerSessionImpl] - exceptionCaught(ServerSessionImpl[null@/127.0.0.1:40886])[state=Opened] IOException: Connection reset by peer

Any advise would be appreciated :slight_smile:

Best regards
Chris

0 Likes

(Rich Koshak) #13

Remember that OH is running as the openhab user.

0 Likes

(Christian) #14

Hi Rich.

Would you mind elaborating how to solve this?
What do I need to do, to make user openhab run this script?
I have chown restartspotify.py to openhab:openhabian, and chmod 0755.

0 Likes

(Christian) #15

I try to copy /.ssh to ~openhab/.ssh but fail in the process. A step by step guide would be great :slight_smile:

0 Likes

(Rich Koshak) #16

Follow the steps in the link.

Try running it with excuteCommandLine and log what is being written by the script. This will tell you what the problem is and give you a clue how to solve it.

User openHAB IS running it when it is executed by openHAB. That’s the point. It’s a different user than when you run the script from the command line. This is almost always the source of problems with exec.

Everyone’s setup is different making a step by step all but impossible.

0 Likes

(Christian) #17

I’ve already posted my openhab.log after executing python script containing executeCommandLine.
I get :

[WARN ] [shd.server.session.ServerSessionImpl] - exceptionCaught(ServerSessionImpl[null@/127.0.0.1:40886])[state=Opened] IOException: Connection reset by peer

I’ve followed the steps in the link, copied .ssh files from openhabian to openhab, but no changes in my results :frowning:
As I can run the command from terminal using openhabian user, I now need to make openhab user permissions to run it to solve my problem.
The exec guide is very good and well written, but I can’t get my head to fully understand how to do this :blush:

0 Likes

(Rich Koshak) #18

Like I already said. You need to use executeCommandLine from a rule and log out what the python script is writing out.

For the purposes of solving your problem, that log statement is irrelevant.

0 Likes

(Christian) #19

I’m sorry Rich, I feel very stupid :smiley: I understand what you mean now. I have generated another rule now with log out from my script. this is what’s returned:

2017-09-18 18:41:36.517 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine '/etc/openhab2/scripts/restartspotify.py'
2017-09-18 18:41:36.568 [INFO ] [ipse.smarthome.model.script.execTest] - 
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified
0 Likes

(Christian) #20

AND now I have managed to corrupt my sudoers file :frowning: the openhabian user is no longer part of sudo group. and I have no privileges to do anything to fix this… Would really like to avoid reinstalling my entire setup, due to privileges issues though :blush:
Have tried almost everything i’ve found on google, without luck

used these tips:

I get this:

openhabian@openHABianPi:~$ sudo openhabian-config
sudo: /etc/sudoers is world writable
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin

0 Likes

(Rich Koshak) #21

Oh boy. You are pretty thoroughly hosed. Not impossibly hosed but your system is in dire straights. Messing up sudoers on a no-longinable-root account system is probably second only to rm -rf / and tied with sudo chmod -R a-x / in single commands that can almost completely disable a system.

In the future and all you future readers of this thread:

ONLY EDIT sudoers USING visudo!

I speak from experience.

You need to boot into some other OS, mount the file system from your server, and re-edit sudoers to make it valid. I have no idea what could be wrong with it. It might just be the file permissions (it should be read/write for root only and no permissions for everyone else).

Since you are running openhabian, you might be able to do this easily if you have a Linux machine or VM that you can mount the SD card to. You can then use visudo -f /path/to/sdcard/sudoers/file. If you have a spare card and USB SD card reader, you might be able to put a stock raspbian on a new SD card and mount the old SD card from the USB reader.

If not, you need to decide if it is going to be more work to set up an environment where you can mount the SD card’s file system or just rebuild the OS from scratch. Of course, if you have files you need off of that SD card, you will have to do this anyway if you don’t have recent backups.

I had an Ubuntu server that I messed up sudoers on and had to boot into a live CD, chroot, and then edit using visudo in order to get my system back. Hopefully, you will have an easier time of it.

BUT, you are on the right track in solving your original problem. :wink:

1 Like

(E. Gerland) #22

I think I got it done almost.
However, when I call:

sudo -u openhab ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_dsa openhab@localhost

I always get the request for entering the passphrase.
How to get rid of this?

0 Likes

(Rich Koshak) #23

Did you configure karaf to use the cert by adding the public key to keys.properties?

Also, I notice you are using dsa. In another thread some users have discovered that karaf doesn’t like dsa and prefers rsa certs.

0 Likes

(E. Gerland) #24

Thanks, Rich

I did the same stuff again with rsa:

sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa -N openhab

and have put the result from openhab.id_rsa.pub into /srv/openhab2-userdata/etc/keys.properties:

openhab=AAAAB3....xyz,_g_:admingroup

sudo -u openhab ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost

It does not ask for the password anymore, but for the passphrase (specified by -N above).
How to get rid of this?

Two questions:

  1. Will this be different, when running the command from OH’s rules (I am the real “openhab” user)?
  2. Is this related to the location of the key files? Means, do I need to put them into a specific folder?

EDIT:
Answer to 1 is NO:

2017-11-30 08:40:33.755 [INFO ] [.smarthome.model.script.system.rules] - Karaf_output: Password authentication^M
Password authentication^M
Password authentication^M
Permission denied, please try again.^M
Permission denied, please try again.^M
Permission denied (keyboard-interactive,password,publickey).

EDIT II:
If I generate the key without a passphrase, it seems to be working in the console:
sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa
(just confirm with enter if passphrase is requested)
entering karaf without password works:
sudo -u openhab ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost
Next I will test it from the rule

0 Likes

(Rich Koshak) #25

When you created the keys, did you supply a password? If I understand what you’ve done so far, if you applied a password to your private key then you will have to enter that password every time you need to authenticate against your public key (i.e. when you log in via ssh), which you discovered in your EDIT II.

Glad you got it to work. I am pretty sure it should work from a rule as well now.

0 Likes

(E. Gerland) #26

It does work on the console with:
ssh -p 8101 -i /home/openhab/karaf_keys/openhab.id_rsa openhab@localhost 'bundle:restart org.openhab.binding.netatmo'

I hope it will do so from OH2 as well…

Thanks for your help

0 Likes

(E. Gerland) #27

Sorry, I did not answer your question.

Yes, previously I provided a passphrase with -N, but after I did it again without it, I was able to run it from bash.

However, it does not run from within OH - and it does not provide any response (same like running it from bash).
I guess I need to go to DEBUG level for all Karaf relevant packages…

EDIT:
I tried this as well - no change:
executeCommandLine("ssh@@-p@@8101@@-i@@/home/openhab/karaf_keys/openhab.id_rsa@@openhab@localhost@@'bundle:restart org.openhab.binding.netatmo'", 6000)

0 Likes