You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-client/bin/cl-client-resume-remount

136 lines
4.2 KiB

#!/usr/bin/env python
9 years ago
# -*- coding: utf-8 -*-
# Copyright 2013-2016 Mir Calculate. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import __future__
import gobject
import dbus
import os
from os import path
import dbus.mainloop.glib
from calculate.lib.datavars import DataVars, VariableError
from calculate.client.client import Client
from argparse import ArgumentParser
from time import sleep
9 years ago
class ResourceRemounter:
"""
Object contains one method for handle 'Resuming' dbus signal.
'handle_resuming' method checks and remounts user remote resources.
Also the object refreshes NSS-cache.
"""
9 years ago
nscd_refresh = "/usr/sbin/nscd-refresh"
def __init__(self, dv):
self.dv = dv
self.dv.defaultModule = 'client'
self.client = Client()
self.client.clVars = dv
self.check_server(True)
9 years ago
def check_server(self, refresh_nscd=False):
"""
Check the available of domain server and refresh nscd cache
"""
if self.client.checkDomainServer(
9 years ago
self.dv.Get('cl_remote_host'),
self.dv.Get('os_net_domain')):
if refresh_nscd and path.exists(self.nscd_refresh):
os.system(self.nscd_refresh)
return True
return False
def remount_remote(self):
"""
Remount remote resource of the domain
"""
try:
self.client.mountRemoteRes(self.dv.Get('cl_remote_pw'),
self.dv.Get('cl_client_remote_path'),
self.dv.Get('cl_remote_host'))
9 years ago
except Exception:
pass
def remount_user_resources(self):
"""
Remount user resource of the domain
"""
try:
self.client.mountUserDomainRes(self.dv.Get('ur_login'),
9 years ago
self.dv.Get('desktop.ur_password'),
int(self.dv.Get('ur_uid')),
int(self.dv.Get('ur_gid')),
"unix", "share", "homes", "ftp")
except Exception:
pass
9 years ago
def handle_resuming(self, statename):
print("Restoring remote mounts for user %s" % self.dv.Get('ur_login'))
# waiting for the domain
9 years ago
for wait in [1, 2, 5, 10]:
try:
if self.check_server(True):
break
9 years ago
except Exception:
pass
sleep(wait)
# check and remount remote resources
self.remount_remote()
self.remount_user_resources()
def main(argv):
# processing the user argument
argp = ArgumentParser(add_help=True)
9 years ago
argp.add_argument("user", help="tracked user", metavar="USER")
namespace = argp.parse_args(argv[1:])
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
# initialization calculate datavars and quitting on an error
try:
dv = DataVars()
dv.importData()
dv.flIniFile()
9 years ago
dv.Set('ur_login', namespace.user)
if dv.Get('desktop.ur_domain_set') == 'off':
9 years ago
print("%s isn't a domain user" % namespace.user)
sys.exit(0)
except VariableError as e:
9 years ago
sys.stderr.write(str(e) + '\n')
sys.exit(1)
rm = ResourceRemounter(dv)
9 years ago
bus.add_signal_receiver(rm.handle_resuming,
dbus_interface="org.freedesktop.UPower",
signal_name="NotifyResume")
loop = gobject.MainLoop()
context = loop.get_context()
while True:
# need for dbus processing
context.iteration(1)
sleep(1)
9 years ago
if __name__ == '__main__':
main(sys.argv)