Restarting CIFS share via API?

Status
Not open for further replies.

George51

Contributor
Joined
Feb 4, 2014
Messages
126
Quick question - and I apologies for any ignorance I am new to this.

I use the API to unlock an encrypted pool - and this works fine (the help I was given for this is in this thread http://forums.freenas.org/index.php?threads/how-do-i-decrypt-my-zfs-drive-via-ssh.17198/)

As I explain in the last few posts of that thread -I have issues when accessing the files from a windows computer - if I unlock them via the GUI it works fine - but using the API (via the script from the other thread) the file is inaccessible until I edit the CIFS share and click ok without making any changes.

Any solution? I dont mind editing the script to restarting the CIFS share? Or figuring out why it doesn't work in the first place.

Kind Regards
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Ok, so it sounds like what oyu need to do is "restart" the CIFS share. I'm not sure how you do that from the API, but a command like "service samba restart" from the CLI should fix it. Give that a try. If you do get updated code with that command and it works, please update this thread and the one you linked to for others.
 

George51

Contributor
Joined
Feb 4, 2014
Messages
126
Ok, so it sounds like what oyu need to do is "restart" the CIFS share. I'm not sure how you do that from the API, but a command like "service samba restart" from the CLI should fix it. Give that a try. If you do get updated code with that command and it works, please update this thread and the one you linked to for others.
that's what I thought. The questions is I have no access to the web GUI most of the time. So how can I automate the service samba restart command? I can automate the API code so ideally the problem will get fixed by adding a bit into it. Does anyone have any clue how to this?
Regards
 

ser_rhaegar

Patron
Joined
Feb 2, 2014
Messages
358
Use SSH with public keys and a script to login and run the proper command.
 

George51

Contributor
Joined
Feb 4, 2014
Messages
126
Use SSH with public keys and a script to login and run the proper command.
I am new to the whole SSH thing - and ideally would like a way to do it via the script i already run. Anyone with any clues to how to do this? Or why it is not working in the first place?

Regards
 

George51

Contributor
Joined
Feb 4, 2014
Messages
126
Or do a periodic cron job.
Something like this could work however it doesn't get unlocked necasarily at a set time - do you know what the command is to restart the CIFS share via a cron job? I may give it a try

Regards
 

George51

Contributor
Joined
Feb 4, 2014
Messages
126
Any hints - I am still struggling with this.

What i am looking for is a way to automate the restarting of a CIFS share, or to find out and fix why it doesn't start correctly using the API

Cheers
 

seeN

Cadet
Joined
Mar 12, 2014
Messages
7
extracted from the startup.py in the api example scripts:
Code:
def service_start(self, name):
        self.request('services/services/%s' % name, method='PUT', data={
            'srv_enable': True,
        })
 
[...]
def run(self):
        self.service_start('cifs')


extracted from the api documentation on services:
List resource
+++++++++++++

.. http:get:: /api/v1.0/services/services/

Returns a list of all available services.

**Example request**:

.. sourcecode:: http

GET /api/v1.0/services/services/ HTTP/1.1
Content-Type: application/json

**Example response**:

.. sourcecode:: http

HTTP/1.1 200 OK
Vary: Accept
Content-Type: application/json

[
{
"srv_service": "rsync",
"id": 16,
"srv_enable": false
},
{
"srv_service": "directoryservice",
"id": 17,
"srv_enable": false
},
{
"srv_service": "smartd",
"id": 18,
"srv_enable": false
}
]

:query offset: offset number. default is 0
:query limit: limit number. default is 30
:resheader Content-Type: content type of the response
:statuscode 200: no error


Update resource
+++++++++++++++

.. http:put:: /api/v1.0/services/services/(int:id|string:srv_service)/

Update service with id `id` or name `srv_service`.

**Example request**:

.. sourcecode:: http

PUT /api/v1.0/services/services/cifs/ HTTP/1.1
Content-Type: application/json

{
"srv_enable": true
}

**Example response**:

.. sourcecode:: http

HTTP/1.1 200 OK
Vary: Accept
Content-Type: application/json

{
"srv_service": "cifs",
"id": 4,
"srv_enable": true
}

:json string srv_service: name of the service
:json boolean srv_enable: service enable
:reqheader Content-Type: the request content type
:resheader Content-Type: the response content type

:statuscode 200: no error
so the key to the solution lies in there since i couldnt find any restart services stuff in the api documentation. but somehow adding a "srv_enable": false for the service in question followed by a "srv_enable": true should work for the services defined. but this does not include the restart of plugins/jails on which i found nothing.
 

seeN

Cadet
Joined
Mar 12, 2014
Messages
7
posted this in http://forums.freenas.org/index.php?threads/how-do-i-decrypt-my-zfs-drive-via-ssh.17198/
can someone merge these two threads since they belong together?

Code:
#!/usr/bin/env python
 
import json
import requests
 
r = requests.put(
 
        'http://<yournas>/api/v1.0/services/services/cifs/',
 
        auth=('root', '<yourpass>'),
 
        headers={'Content-Type': 'application/json'},
 
        verify=False,
 
        data=json.dumps({'srv_enable': True}),
 
)
 
print(r.text)

edit <words> - if your smb is disabled then this otherwise disable with False then this.
 

George51

Contributor
Joined
Feb 4, 2014
Messages
126
Code:
import json
import requests
import time
r = requests.post(
    'http://192.168.*.***/api/v1.0/storage/volume/Family/unlock/',
    auth=('root','****'),
    headers={'Content-Type': 'application/json'},
    verify=False,
    data=json.dumps({'passphrase': '****'})
    )
time.sleep(60)
s = requests.put(
    'http://192.168.*.***/api/v1.0/services/services/cifs/',
    auth=('root','****'),
    headers={'Content-Type': 'application/json'},
    verify=False,
    data=json.dumps({'srv_enable': False}),
    )
time.sleep(10),
t = requests.put(
    'http://192.168.*.***/api/v1.0/services/services/cifs/',
    auth=('root','****'),
    headers={'Content-Type': 'application/json'},
    verify=False,
    data=json.dumps({'srv_enable': True}),
    )


Okay so that is what I use to unlock the encrypted pool, and restart CIFS so that I can access it of my windows computers. This works absolutely fine and thank you to those that helped get to this stage. The next (minor) issue is I use windows scheduler to run this every time someone logs onto one of my computers. And therefore every time that happens the CIFS gets restated and whoever is accessing it from elsewhere gets disconnected. This is not ideal, I have tried to schedule it just once, waking the computer up at a given time to run the script. But despite lots of research I couldn't get the computer to wake up via the BIOS. Hence why I ended up doing it when someone logs on.

The solutions I can see - schedule it an entirely different way or have a line that checks to see if the pool is unlocked, and if it is stops the rest of the script running. Help with either would be appreciated
 
Status
Not open for further replies.
Top