Example #1
0
        def decorator(*args, **kwargs):
            if len(args) > 0 and hasattr(args[0], "log"):
                log = args[0].log
            else:
                log = None

            if len(args) > 0 and hasattr(args[0], "cache_sig_prefix"):
                _sig = args[0].cache_sig_prefix + sig
            else:
                _sig = sig.format(args=args, kwargs=kwargs)

            fpath = cache_fpath(_sig)

            try:
                lfd = lock.lock(timeout=30, delay=0.1, lockfile=fpath + '.lock', intent="cache")
            except Exception as e:
                if log:
                    log.warning("cache locking error: %s. run command uncached." % str(e))
                return fn(*args, **kwargs)
            try:
                data = cache_get(fpath, log=log)
            except Exception as e:
                if log:
                    log.debug(str(e))
                data = fn(*args, **kwargs)
                cache_put(fpath, data, log=log)
            lock.unlock(lfd)
            return data
Example #2
0
    def provisioner(self, need_boot=True):
        """provision zone
        - configure zone
        - if snapof and zone brand is native
           then create zonepath from snapshot of snapof
           then attach zone
        - if snapof and zone brand is ipkg
           then try to detect zone associated with snapof
           then define container_origin
        - if container_origin
           then clone  container_origin
        - create sysidcfg
        - if need_boot boot and wait multiuser
        """
        self.osver = get_solaris_version()
        self.zone_configure()

        if self.osver >= 11:
            self.create_sysidcfg(self.r)
        else:
            if self.snapof is not None and self.r.brand == 'native':
                self.create_zonepath()
                self.r.zoneadm("attach", ["-F"])
            elif self.snapof is not None and self.r.brand == 'ipkg':
                zones = rcZone.Zones()
                src_dataset = Dataset(self.snapof)
                zonepath = src_dataset.getprop('mountpoint')
                self.container_origin = zones.zonename_from_zonepath(
                    zonepath).zonename
                self.log.info("source zone is %s (detected from snapof %s)" %
                              (self.container_origin, self.snapof))

        if self.container_origin is not None:
            lockname = 'create_zone2clone-' + self.container_origin
            lockfile = os.path.join(rcEnv.paths.pathlock, lockname)
            self.log.info("wait get lock %s" % (lockname))
            try:
                lockfd = lock.lock(timeout=1200, delay=5, lockfile=lockfile)
            except:
                raise (ex.excError("failure in get lock %s" % (lockname)))
            try:
                self.create_zone2clone()
            except:
                lock.unlock(lockfd)
                raise
            lock.unlock(lockfd)
            self.create_cloned_zone()

        if self.osver < 11:
            self.create_sysidcfg(self.r)

        if need_boot is True:
            self.r.zone_boot()
            self.r.wait_multi_user()

        self.r.log.info("provisioned")
        return True
Example #3
0
def daemon_test_lock():
    lockfd = None
    try:
        lockfd = lock.lock(lockfile=rcEnv.paths.daemon_lock, timeout=0, delay=0)
    except:
        return True
    finally:
        lock.unlock(lockfd)
    return False
Example #4
0
 def unlock(self):
     """
     Release the app action lock.
     """
     if not self.lockfd:
         return
     self.log.debug("release app lock")
     lock.unlock(self.lockfd)
     try:
         os.unlink(self.lockfile)
     except OSError:
         pass
     self.lockfd = None
Example #5
0
def clear_cache(sig, o=None):
    if o and hasattr(o, "cache_sig_prefix"):
        sig = o.cache_sig_prefix + sig
    fpath = cache_fpath(sig)
    if not os.path.exists(fpath):
        return
    if o and hasattr(o, "log"):
        o.log.debug("cache CLEAR: %s" % fpath)
    lfd = lock.lock(timeout=30, delay=0.1, lockfile=fpath + '.lock')
    try:
        os.unlink(fpath)
    except:
        pass
    lock.unlock(lfd)
Example #6
0
def process(command):
    global data_is_encrypted
    if (command == 'exit'):
        conn.send("Exiting...\n")
        close_all()
    elif (command == 'encrypt'):
        data_is_encrypted = True
        return "Using Encrypted Commands now.\n"
    elif (command == 'plain'):
        data_is_encrypted = False
        return "Using Plain-Text Commands now.\n"
    elif (command == 'reboot please'):
        reboot()
        return "You asked for it.\n"
    elif (command == 'reboot'):
        return "If you ask nicely.\n"
    elif (command == 'lock'):
        return lock.lock() + "\n"
    elif (command == 'unlock'):
        return lock.unlock() + "\n"
    elif (command == 'attempt'):
        return "Attempt " + str(attempt_num) + "\n"
    elif (command == 'hello'):
        return "Hi.\n"
    else:
        return command + ' is not a recognized command\n'
Example #7
0
def web_unlock():
    if not (request.args.get("token") and request.args.get("state")):
        return "Error"
    else:
        with shelve.open("Settings.conf") as settings:
            if "token" in settings:
                token = settings["token"]
            else:
                return "System not setup !"
        if request.args.get("token") != token:
            return "Invalid Token"
        if request.args.get("state") == "open":
            lock.unlock()
        elif request.args.get("state") == "close":
            lock.lock()
        elif request.args.get("state") == "switch":
            lock.switch()
        else:
            return "Invalid State"
    return "Done"
Example #8
0
def f_unlock():
    lock.unlock()
Example #9
0
 def unlock(self):
     import lock
     lock.unlock(self.lockfd)
Example #10
0
 def unlock(self):
     if self.lockfd:
         unlock(self.lockfd)
     self.set_last_shutdown()
Example #11
0
 def test_lock_unlock(tmp_file, timeout):
     assert os.path.exists(tmp_file) is False
     lock_fd = lock.lock(lockfile=tmp_file, timeout=timeout, intent="test")
     assert os.path.exists(tmp_file) is True
     lock.unlock(lock_fd)
Example #12
0
 def resunlock(self):
     """
     Release the service action lock.
     """
     lock.unlock(self.lockfd)
     self.lockfd = None
Example #13
0
 def unlock(self):
     """
     Release the startip lock.
     """
     lock.unlock(self.lockfd)
Example #14
0
    def docker_start(self, verbose=True):
        """
        Start the docker daemon if in private mode and not already running.
        """
        if not self.docker_daemon_private:
            return
        if self.docker_cmd is None:
            raise ex.excError("docker executable not found")
        import lock
        lockfile = os.path.join(self.svc.var_d, "lock.docker_start")
        try:
            lockfd = lock.lock(timeout=15, delay=1, lockfile=lockfile)
        except lock.LOCK_EXCEPTIONS as exc:
            self.svc.log.error("dockerd start lock acquire failed: %s",
                               str(exc))
            return

        # Sanity checks before deciding to start the daemon
        if self.docker_running():
            lock.unlock(lockfd)
            return

        if self.container_data_dir is None:
            lock.unlock(lockfd)
            return

        resource = self._container_data_dir_resource()
        if resource is not None:
            state = resource._status()
            if state not in (rcStatus.UP, rcStatus.STDBY_UP):
                self.svc.log.warning(
                    "the docker daemon data dir is handled by the %s "
                    "resource in %s state. can't start the docker "
                    "daemon", resource.rid, rcStatus.Status(state))
                lock.unlock(lockfd)
                return

        if os.path.exists(self.docker_pid_file):
            self.svc.log.warning("removing leftover pid file %s",
                                 self.docker_pid_file)
            os.unlink(self.docker_pid_file)

        # Now we can start the daemon, creating its data dir if necessary
        cmd = self.dockerd_cmd

        if verbose:
            self.svc.log.info("starting docker daemon")
            self.svc.log.info(" ".join(cmd))
        import subprocess
        subprocess.Popen(
            ["nohup"] + cmd,
            stdout=open("/dev/null", "w"),
            stderr=open("/dev/null", "a"),
            preexec_fn=os.setpgrp,
            close_fds=True,
        )

        import time
        try:
            for _ in range(self.max_wait_for_dockerd):
                if self._docker_working():
                    self.clear_daemon_caches()
                    return
                time.sleep(1)
        finally:
            lock.unlock(lockfd)
Example #15
0
 def unlock(self):
     if self.lockfd is None:
         return
     lock.unlock(self.lockfd)
     self.lockfd = None