os.

Here are the examples of the python api os. taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

200 Examples 7

Example 151

View license
    def __init__(self, domain, commonName=None, security_dir=None, force=False, readonly=False):
        self.configfile = None
        self.commonName = commonName or 'runtime'
        self.config = ConfigParser.SafeConfigParser()
        self.config.optionxform = str
        os.umask(0077)

Example 152

Project: autotest
Source File: build_externals.py
View license
def main():
    """
    Find all ExternalPackage classes defined in this file and ask them to
    fetch, build and install themselves.
    """
    logging_manager.configure_logging(BuildExternalsLoggingConfig(),
                                      verbose=True)
    os.umask(022)

Example 153

Project: Observatory
Source File: Repository.py
View license
def clone_svn_repo(clone_url, destination_dir, fresh_clone = False):
  if fresh_clone:
    # make the repo's directory
    try:
      os.makedirs(destination_dir, 0770)
    except OSError as e:
      pass
    
    clone_cmdline = ["git", "svn", "clone", clone_url, destination_dir]
  else:
    clone_cmdline = ["git", "svn", "fetch"]
  
  if subprocess.call(clone_cmdline, cwd = destination_dir) != 0:
    raise Repository.CheckoutFailureException(" ".join(clone_cmdline))

Example 154

Project: botornado
Source File: credentials.py
View license
    def save(self, file_path):
        """
        Persist a Session Token to a file in JSON format.

        :type path: str
        :param path: The fully qualified path to the file where the
            the Session Token data should be written.  Any previous
            data in the file will be overwritten.  To help protect
            the credentials contained in the file, the permissions
            of the file will be set to readable/writable by owner only.
        """
        fp = open(file_path, 'wb')
        json.dump(self.to_dict(), fp)
        fp.close()
        os.chmod(file_path, 0600)

Example 155

Project: RSqueak
Source File: profiler_plugin.py
View license
    def open(self, name):
        self.fd = os.open(name, os.O_RDWR | os.O_CREAT | O_BINARY, 0666)

Example 156

Project: cloudbiolinux
Source File: ec2autorun.py
View license
def _get_boot_script(ud):
    # Test if cluster bucket exists; if it does not, resort to the default
    # bucket for downloading the boot script
    use_default_bucket = ud.get("use_default_bucket", False)
    if ud.has_key('bucket_default'):
        default_bucket_name = ud['bucket_default']
    else:
        default_bucket_name = DEFAULT_BUCKET_NAME
    if not use_default_bucket and ud.has_key('bucket_cluster') and ud['access_key'] is not None and ud['secret_key'] is not None:
        s3_conn = _get_s3_conn(ud)
        # Check if cluster bucket exists or use the default one
        if not _bucket_exists(s3_conn, ud['bucket_cluster']) or \
           not _remote_file_exists(s3_conn, ud['bucket_cluster'], ud['boot_script_name']):
            log.debug("Using default bucket '%s'" % default_bucket_name)
            use_default_bucket = True
        else:
            log.debug("Using cluster bucket '%s'" % ud['bucket_cluster'])
            use_default_bucket = False
    else:
        log.debug("bucket_clutser not specified or no credentials provided; defaulting to bucket '%s'"
            % default_bucket_name)
        use_default_bucket = True

    # If using cluster bucket, use credentials because the boot script may not be accessible to everyone
    got_boot_script = False
    if use_default_bucket is False:
        log.debug("Trying to get boot script '%s' from cluster bucket '%s'"
            % (ud['boot_script_name'], ud.get('bucket_cluster', None)))
        got_boot_script = _get_file_from_bucket(s3_conn, ud['bucket_cluster'], ud['boot_script_name'],
            DEFAULT_BOOT_SCRIPT_NAME)
        if got_boot_script:
            os.chmod(os.path.join(LOCAL_PATH, DEFAULT_BOOT_SCRIPT_NAME), 0744)
    # If did not get the boot script, fall back on the publicly available one
    if not got_boot_script or use_default_bucket:
        boot_script_url = os.path.join(_get_default_bucket_url(ud), ud.get('boot_script_name',
            DEFAULT_BOOT_SCRIPT_NAME))
        log.debug("Could not get boot script '%s' from cluster bucket '%s'; "
            "retrieving the public one from bucket url '%s'" \
            % (ud['boot_script_name'], ud.get('bucket_cluster', None), boot_script_url))
        got_boot_script = _get_file_from_url(boot_script_url)
    if got_boot_script:
        log.debug("Saved boot script to '%s'" % os.path.join(LOCAL_PATH, DEFAULT_BOOT_SCRIPT_NAME))
        # Save the downloaded boot script to cluster bucket for future invocations
        use_object_store = ud.get("use_object_store", True)
        if use_object_store and ud.has_key('bucket_cluster') and ud['bucket_cluster']:
            s3_conn = _get_s3_conn(ud)
            if _bucket_exists(s3_conn, ud['bucket_cluster']) and \
               not _remote_file_exists(s3_conn, ud['bucket_cluster'], ud['boot_script_name']):
                _save_file_to_bucket(s3_conn, ud['bucket_cluster'], ud['boot_script_name'], \
                    DEFAULT_BOOT_SCRIPT_NAME)
        return True
    log.debug("**Could not get the boot script**")
    return False

Example 157

Project: deltaic
Source File: rbd.py
View license
def create_or_open(path):
    # Open a file R/W, creating it if necessary but not truncating it.
    fd = os.open(path, os.O_RDWR | os.O_CREAT, 0666)
    return os.fdopen(fd, 'r+b')

Example 158

Project: mysql
Source File: manage.py
View license
@debug(log_output=True)
def assert_initialized_for_state(node):
    """
    If the node has not yet been set up, find the correct state and
    initialize for that state. After the first health check we'll have
    written a lock file and will never hit this path again.
    """
    LOCK_PATH = '/var/run/init.lock'
    try:
        os.mkdir(LOCK_PATH, 0700)
    except OSError:
        # the lock file exists so we've already initialized
        return True

    # the check for primary will set the state if its known. If another
    # instance is the primary then we'll be marked as REPLICA, so if
    # we can't determine after the check which we are then we're likely
    # the first instance (this will get safely verified later).
    if node.is_primary() or node.cp.state == UNASSIGNED:
        try:
            if not run_as_primary(node):
                log.error('Tried to mark node %s primary but primary exists, '
                          'exiting for retry on next check.', node.name)
                os.rmdir(LOCK_PATH)
                sys.exit(1)
        except MySQLError as ex:
            # We've made it only partly thru setup. Setup isn't idempotent
            # but should be safe to retry if we can make more progress. At
            # worst we end up with a bunch of failure logs.
            log.error('Failed to set up %s as primary (%s). Exiting but will '
                      'retry setup. Check logs following this line to see if '
                      'setup needs reconfiguration or manual intervention to '
                      'continue.', node.name, ex)
            os.rmdir(LOCK_PATH)
            sys.exit(1)
        if node.cp.update():
            node.cp.reload()
            # this is racy with the SIGHUP that ContainerPilot just got
            # sent, but if the Consul agent shuts down quickly enough we
            # end up sending extra API calls to it and get a bunch of log
            # spam. This forces us to exit early.
            sys.exit(0)
    else:
        try:
            run_as_replica(node)
        except (UnknownPrimary, MySQLError) as ex:
            log.error('Failed to set up %s for replication (%s). Exiting for retry '
                      'on next check.', node.name, ex)
            os.rmdir(LOCK_PATH)
            sys.exit(1)
    return False

Example 159

Project: flumotion
Source File: stream.py
View license
    def do(self, args):
        '''Check URL, IP and perfom the stream check'''
        if not args:
            return self.critical('Please specify the url to check the '
            'stream/playlist.')

        # Check url and get path from it
        self._url = args[0]
        try:
            parse = urlparse.urlparse(self._url)
        except ValueError:
            return self.critical('URL isn\'t valid.')
        if parse[0] != 'http':
            return self.critical('URL type is not valid.')
        self._path = parse[2]

        # use unique names for stream dumps
        if len(self._path) <= 1:
            self._path = 'unknown'
        elif self._path[0] == '/':
            self._path = self._path[1:]
        slug = self._path.replace('/', '_')
        if slug[-3:] in PLAYLIST_SUFFIX:
            slug = slug[:-4]
        (fd, self._tmpfile) = tempfile.mkstemp(
            suffix='.flumotion-nagios.%s-%s' % (
                datetime.datetime.now().strftime('%Y%m%dT%H%M%S'), slug))
        # make the dump file group readable
        os.chmod(self._tmpfile, 0640)

        if self.options.bouncer:
            # Check for a valid IPv4 address with numbers and dots
            parts = self.options.ip.split('.')
            if len(parts) != 4:
                return self.critical('URL type is not valid.')
            for item in parts:
                if not 0 <= int(item) <= 255:
                    return self.critical('URL type is not valid.')
            result = self.connect(self.options)
            reactor.run()
            if reactor.exitStatus != 0:
                sys.exit(reactor.exitStatus)

        # Determine if this stream would have audio/video
        if self.options.videowidth or self.options.videoheight or \
           self.options.videoframerate or self.options.videopar or \
           self.options.videomimetype:
            self._expectVideo = True
        if self.options.audiosamplerate or self.options.audiochannels or \
           self.options.audiowidth or self.options.audiodepth or \
           self.options.audiomimetype:
            self._expectAudio = True

        # Simple playlist detection
        p = urlparse.urlparse(self._url)
        (tmpfd, tmpPath) = tempfile.mkstemp()
        tmp = os.fdopen(tmpfd, 'wb')
        if p[2][-4:] == "m3u8":
            self._url = getURLFromPlaylist(self._url)
            p = urlparse.urlparse(self._url)
            if p[2][-4:] == "m3u8":
                self._url = getURLFromPlaylist(self._url)
            self.options.playlist = True
            try:
                segment = urlOpenWithCookies(self._url)
            except urllib2.HTTPError, e:
                # For fragmented streams, a 404 is not critical
                if e.code == 404:
                    return self.warning("Fetching segment returned "
                        "\"%s\". The stream might be out of sync." % e)
                raise
            c = segment.read()
            tmp.write(c)
            tmp.close()
            self._url = "file://%s" % (tmpPath, )
        elif not self.options.playlist and self._url[-3:] in PLAYLIST_SUFFIX:
            self._url = getURLFromPlaylist(self._url)
            self.options.playlist = True
        # If it is a playlist, take the correct URL
        elif self.options.playlist:
            self._url = getURLFromPlaylist(self._url)

        result = self.checkStream()
        os.unlink(tmpPath)
        return result

Example 160

Project: django-lean
Source File: lockfile.py
View license
def openlock(filename, operation, wait=True):
    """
    Returns a file-like object that gets a fnctl() lock.

    `operation` should be one of LOCK_SH or LOCK_EX for shared or
    exclusive locks.

    If `wait` is False, then openlock() will not block on trying to
    acquire the lock.
    """
    f = os.fdopen(os.open(filename, os.O_RDWR | os.O_CREAT, 0666), "r+")
    if not wait:
        operation |= LOCK_NB
    try:
        lockf(f.fileno(), operation)
    except IOError, err:
        if not wait and err.errno in (EACCES, EAGAIN):
            from django.core.management.base import CommandError
            raise CommandError("Could not acquire lock on '%s' held by %s." %
                               (filename, f.readline().strip()))
        raise
    print >>f, "%s:%d" % (socket.gethostname(), os.getpid())
    f.truncate()
    f.flush()
    return f

Example 161

View license
    def test_deployedMode(self):
        """
        The C{dropin.cache} file may not be writable: the cache should still be
        attainable, but an error should be logged to show that the cache
        couldn't be updated.
        """
        # Generate the cache
        plugin.getCache(self.module)

        cachepath = self.package.child('dropin.cache')

        # Add a new plugin
        FilePath(__file__).sibling('plugin_extra1.py'
            ).copyTo(self.package.child('pluginextra.py'))

        os.chmod(self.package.path, 0500)
        # Change the right of dropin.cache too for windows
        os.chmod(cachepath.path, 0400)
        self.addCleanup(os.chmod, self.package.path, 0700)
        self.addCleanup(os.chmod, cachepath.path, 0700)

        # Start observing log events to see the warning
        events = []
        addObserver(events.append)
        self.addCleanup(removeObserver, events.append)

        cache = plugin.getCache(self.module)
        # The new plugin should be reported
        self.assertIn('pluginextra', cache)
        self.assertIn(self.originalPlugin, cache)

        # Make sure something was logged about the cache.
        expected = "Unable to write to plugin cache %s: error number %d" % (
            cachepath.path, errno.EPERM)
        for event in events:
            if expected in textFromEventDict(event):
                break
        else:
            self.fail(
                "Did not observe unwriteable cache warning in log "
                "events: %r" % (events,))

Example 162

View license
    def test_deployedMode(self):
        """
        The C{dropin.cache} file may not be writable: the cache should still be
        attainable, but an error should be logged to show that the cache
        couldn't be updated.
        """
        # Generate the cache
        plugin.getCache(self.module)

        cachepath = self.package.child('dropin.cache')

        # Add a new plugin
        FilePath(__file__).sibling('plugin_extra1.py'
            ).copyTo(self.package.child('pluginextra.py'))

        os.chmod(self.package.path, 0500)
        # Change the right of dropin.cache too for windows
        os.chmod(cachepath.path, 0400)
        self.addCleanup(os.chmod, self.package.path, 0700)
        self.addCleanup(os.chmod, cachepath.path, 0700)

        # Start observing log events to see the warning
        events = []
        addObserver(events.append)
        self.addCleanup(removeObserver, events.append)

        cache = plugin.getCache(self.module)
        # The new plugin should be reported
        self.assertIn('pluginextra', cache)
        self.assertIn(self.originalPlugin, cache)

        # Make sure something was logged about the cache.
        expected = "Unable to write to plugin cache %s: error number %d" % (
            cachepath.path, errno.EPERM)
        for event in events:
            if expected in textFromEventDict(event):
                break
        else:
            self.fail(
                "Did not observe unwriteable cache warning in log "
                "events: %r" % (events,))

Example 163

Project: termite-data-server
Source File: fileutils.py
View license
def _extractall(filename, path='.', members=None):
    # FIXME: this should be dropped because python 2.4 support was dropped
    if not hasattr(tarfile.TarFile, 'extractall'):
        from tarfile import ExtractError

        class TarFile(tarfile.TarFile):

            def extractall(self, path='.', members=None):
                """Extract all members from the archive to the current working
                directory and set owner, modification time and permissions on
                directories afterwards. `path' specifies a different directory
                to extract to. `members' is optional and must be a subset of the
                list returned by getmembers().
                """

                directories = []
                if members is None:
                    members = self
                for tarinfo in members:
                    if tarinfo.isdir():

                        # Extract directory with a safe mode, so that
                        # all files below can be extracted as well.

                        try:
                            os.makedirs(os.path.join(path,
                                                     tarinfo.name), 0777)
                        except EnvironmentError:
                            pass
                        directories.append(tarinfo)
                    else:
                        self.extract(tarinfo, path)

                # Reverse sort directories.

                directories.sort(lambda a, b: cmp(a.name, b.name))
                directories.reverse()

                # Set correct owner, mtime and filemode on directories.

                for tarinfo in directories:
                    path = os.path.join(path, tarinfo.name)
                    try:
                        self.chown(tarinfo, path)
                        self.utime(tarinfo, path)
                        self.chmod(tarinfo, path)
                    except ExtractError, e:
                        if self.errorlevel > 1:
                            raise
                        else:
                            self._dbg(1, 'tarfile: %s' % e)

        _cls = TarFile
    else:
        _cls = tarfile.TarFile

    tar = _cls(filename, 'r')
    ret = tar.extractall(path, members)
    tar.close()
    return ret

Example 164

Project: termite-visualizations
Source File: fileutils.py
View license
def _extractall(filename, path='.', members=None):
    if not hasattr(tarfile.TarFile, 'extractall'):
        from tarfile import ExtractError

        class TarFile(tarfile.TarFile):

            def extractall(self, path='.', members=None):
                """Extract all members from the archive to the current working
             directory and set owner, modification time and permissions on
             directories afterwards. `path' specifies a different directory
             to extract to. `members' is optional and must be a subset of the
             list returned by getmembers().
                """

                directories = []
                if members is None:
                    members = self
                for tarinfo in members:
                    if tarinfo.isdir():

                        # Extract directory with a safe mode, so that
                        # all files below can be extracted as well.

                        try:
                            os.makedirs(os.path.join(path,
                                                     tarinfo.name), 0777)
                        except EnvironmentError:
                            pass
                        directories.append(tarinfo)
                    else:
                        self.extract(tarinfo, path)

                # Reverse sort directories.

                directories.sort(lambda a, b: cmp(a.name, b.name))
                directories.reverse()

                # Set correct owner, mtime and filemode on directories.

                for tarinfo in directories:
                    path = os.path.join(path, tarinfo.name)
                    try:
                        self.chown(tarinfo, path)
                        self.utime(tarinfo, path)
                        self.chmod(tarinfo, path)
                    except ExtractError, e:
                        if self.errorlevel > 1:
                            raise
                        else:
                            self._dbg(1, 'tarfile: %s' % e)

        _cls = TarFile
    else:
        _cls = tarfile.TarFile

    tar = _cls(filename, 'r')
    ret = tar.extractall(path, members)
    tar.close()
    return ret

Example 165

Project: rutracker.py
Source File: rutracker.py
View license
    def save(self):
        configFile = os.fdopen(os.open(self._getConfigPath(), os.O_WRONLY | os.O_CREAT, 0600), 'w')
        self.__config.write(configFile)

Example 166

Project: ganeti
Source File: io.py
View license
def WritePidFile(pidfile):
  """Write the current process pidfile.

  @type pidfile: string
  @param pidfile: the path to the file to be written
  @raise errors.LockError: if the pid file already exists and
      points to a live process
  @rtype: int
  @return: the file descriptor of the lock file; do not close this unless
      you want to unlock the pid file

  """
  # We don't rename nor truncate the file to not drop locks under
  # existing processes
  fd_pidfile = os.open(pidfile, os.O_RDWR | os.O_CREAT, 0600)

  # Lock the PID file (and fail if not possible to do so). Any code
  # wanting to send a signal to the daemon should try to lock the PID
  # file before reading it. If acquiring the lock succeeds, the daemon is
  # no longer running and the signal should not be sent.
  try:
    filelock.LockFile(fd_pidfile)
  except errors.LockError:
    msg = ["PID file '%s' is already locked by another process" % pidfile]
    # Try to read PID file
    pid = _ParsePidFileContents(os.read(fd_pidfile, 100))
    if pid > 0:
      msg.append(", PID read from file is %s" % pid)
    raise errors.PidFileLockError("".join(msg))

  os.write(fd_pidfile, "%d\n" % os.getpid())

  return fd_pidfile

Example 167

Project: sonospy
Source File: fileutils.py
View license
def _extractall(filename, path='.', members=None):
    if not hasattr(tarfile.TarFile, 'extractall'):
        from tarfile import ExtractError

        class TarFile(tarfile.TarFile):

            def extractall(self, path='.', members=None):
                """Extract all members from the archive to the current working
             directory and set owner, modification time and permissions on
             directories afterwards. `path' specifies a different directory
             to extract to. `members' is optional and must be a subset of the
             list returned by getmembers().
                """

                directories = []
                if members is None:
                    members = self
                for tarinfo in members:
                    if tarinfo.isdir():

                        # Extract directory with a safe mode, so that
                        # all files below can be extracted as well.

                        try:
                            os.makedirs(os.path.join(path,
                                    tarinfo.name), 0777)
                        except EnvironmentError:
                            pass
                        directories.append(tarinfo)
                    else:
                        self.extract(tarinfo, path)

                # Reverse sort directories.

                directories.sort(lambda a, b: cmp(a.name, b.name))
                directories.reverse()

                # Set correct owner, mtime and filemode on directories.

                for tarinfo in directories:
                    path = os.path.join(path, tarinfo.name)
                    try:
                        self.chown(tarinfo, path)
                        self.utime(tarinfo, path)
                        self.chmod(tarinfo, path)
                    except ExtractError, e:
                        if self.errorlevel > 1:
                            raise
                        else:
                            self._dbg(1, 'tarfile: %s' % e)


        _cls = TarFile
    else:
        _cls = tarfile.TarFile

    tar = _cls(filename, 'r')
    ret = tar.extractall(path, members)
    tar.close()
    return ret

Example 168

Project: pybbs
Source File: BRead.py
View license
    def Init(self):
        if self._cache_map == None:
            cachepath = User.User.CacheFile(self._userid, '')
            try:
                os.mkdir(cachepath, 0700)
            except:
                pass
            entrypath = User.User.CacheFile(self._userid, 'entry')
            try:
                os.stat(entrypath)
            except:
#                Log.debug("no brc cache file for %s, creating" % self._userid)
                brc = '\0' * BRC_CACHE_NUM * BrcCacheEntry.size
                fbrc = os.open(entrypath, os.O_RDWR | os.O_CREAT, 0600)
                os.write(fbrc, brc)
                os.close(fbrc)
            fbrc = open(entrypath, "r+b")
            if (fbrc == None):
                Log.error("cannot init brc cache for %s" % self._userid)
                return False
            self._cache_map = mmap.mmap(fbrc.fileno(), BRC_CACHE_NUM * BrcCacheEntry.size, prot = mmap.PROT_READ | mmap.PROT_WRITE, flags = mmap.MAP_SHARED)
            fbrc.close()
            if (self._cache_map == None):
                Log.error("failed to mmap cache file for %s" % self._userid)
                return False

            self._cache = [0] * BRC_CACHE_NUM
            for i in range(0, BRC_CACHE_NUM):
                self._cache[i] = BrcCacheEntry(self, i)

        return True

Example 169

Project: spinnaker
Source File: google_install_loader.py
View license
def __unpack_and_run():
    """Unpack the files from metadata, and run the main script.

    This is intended to be used where a startup script needs a bunch
    of different files for a startup script that is passed through metadata
    in a GCE instance.

    The actual startup acts like a bootloader that unpacks all the
    files from the metadata, then passes control the specific startup script
    for the installation.

    The bootloader unpacks the files mentioned in the 'startup_loader_files'
    metadata, which is a space-delimited list of other metadata keys that
    contain the files. Because the keys cannot contain '.', we encode the
    filenames as <ext>_<basename> using the leading '_' to separate the
    extension, whichi s added as a prefix.

    The true startup script is denoted by the 'startup_command' attribute,
    which specifies the name of a python script to run (presumably packed
    into the startup_loader_files). The script uses the unencoded name once
    unpacked. The python command itself is ommited and will be added here.
    """

    script_keys = get_instance_metadata_attribute('startup_loader_files')
    key_list = script_keys.split('+') if script_keys else []
    unpack_files(key_list)
    if script_keys:
      clear_instance_metadata('startup_loader_files')

    startup_command = get_instance_metadata_attribute('startup_command')
    if not startup_command:
        sys.stderr.write('No "startup_command" metadata key.\n')
        raise SystemExit('No "startup_command" metadata key.')

    # Change the startup script to the final command that we run
    # so that future boots will just run that command. And take down
    # the rest of the boostrap metadata since we dont need it anymore.
    command = ('chmod gou+rx /opt/spinnaker/install/*.sh; '
               + startup_command.replace('+', ' '))

    with open('__startup_script__.sh', 'w') as f:
        f.write('#!/bin/bash\ncd /opt/spinnaker/install\n{command}\n'
                .format(command=command))

    os.chmod('__startup_script__.sh', 0555)

Example 170

Project: scons
Source File: TestSCons_time.py
View license
    def write_fake_svn_py(self, name):
        name = self.workpath(name)
        self.write(name, svn_py)
        os.chmod(name, 0755)

Example 171

Project: boatshoes
Source File: SingleInstance.py
View license
    def try_pid_lock(self, lockfile):
        # Open the lockfile
        try:
            # Need the low-levelness to make sure the file gets created
            #   with the right mode.
            fd = os.open(lockfile, os.O_RDWR | os.O_CREAT, 0644)
        except OSError, e:
            if e.errno == errno.EACCES:
                raise PidPermissionError("Permission denied when trying to "
                                         "open the pidfile.")
            raise SingleInstanceError("Couldn't open lockfile %s: %s" %
                                      (lockfile, e.strerror))
        # Lock the lockfile
        try:
            fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError, e:
            os.close(fd)
            # If one of these errors, then its already locked
            if (e.errno == errno.EACCES or
                e.errno == errno.EAGAIN):
                raise RunningInstanceError("Can't lock file. "
                                           "Is another instance running?")
            # Otherwise, its a fcntl error
            raise SingleInstanceError("Can't lock the lockfile %s: %s" %
                                      (lockfile, e.strerror))
        # At this point, the file is locked. Write in the pid and return.
        # Clear the contents of the file
        try:
            os.ftruncate(fd, 0)
        except OSError, e:
            os.close(fd)
            raise SingleInstanceError("Can't truncate the lockfile %s: %s" %
                                      (lockfile, e.strerror))
        # Write the PID
        #f = os.fdopen(fd)
        #f.write(os.getpid());
        os.write(fd, str(os.getpid()))
        # keep the fd open!
        self._file = fd

Example 172

Project: fedup
Source File: upgrade.py
View license
    def openpipe(self):
        log.debug("creating log pipe")
        pipefile = tempfile.mktemp(prefix='rpm-log-pipe.')
        os.mkfifo(pipefile, 0600)

Example 173

Project: karesansui
Source File: uriguestby1status.py
View license
    @auth
    def _PUT(self, *param, **params):
        """<comment-ja>
        ステータス更新
         - param
           - create = 0
           - shutdown = 1
           - destroy = 2
           - suspend = 3
           - resume = 4
           - reboot = 5
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """

        host_id =  param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id =  param[1]
        if uri_id is None:
            return web.notfound()

        if not validates_uriguest_status(self):
            return web.badrequest(self.view.alert)

        status = int(self.input.status)

        model = findbyhost1(self.orm, host_id)

        if model.attribute == 2:
            info = {}
            segs = uri_split(model.hostname)
            uri = uri_join(segs, without_auth=True)
            creds = ''
            if segs["user"] is not None:
                creds += segs["user"]
                if segs["passwd"] is not None:
                    creds += ':' + segs["passwd"]
            self.kvc = KaresansuiVirtConnectionAuth(uri,creds)

            try:
                host = MergeHost(self.kvc, model)
                for guest in host.guests:
                    _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                    if 0 < len(_virt):
                        for _v in _virt:
                            info = _v.get_info()
                            #uri = _v._conn.getURI()
                            if info["uuid"] == uri_id or (uri[0:5] == "test:"):

                                esc_name = "'%s'" % guest.info["model"].name
                                opts = {"name":esc_name,"connection":uri}

                                if creds != '':
                                    passwd_file = KARESANSUI_TMP_DIR + "/" + segs['host'] + ".auth"
                                    open(passwd_file, "w").write(creds)
                                    os.chmod(passwd_file, 0600)
                                    opts["passwd-file"] = passwd_file

                                if status == GUEST_ACTION_CREATE:
                                    # -- Create
                                    cmdname = ["Start Guest", "start guest"]
                                    if _v.is_creatable() is True:
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_START_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_START_GUEST
                                    else:
                                        self.logger.error("Create Action:The state can not run. - %d" % _v.status())
                                    
                                elif status == GUEST_ACTION_SHUTDOWN:
                                    cmdname = ["Shutdown Guest", "shutdown guest"]
                                    if _v.is_shutdownable() is True:
                                        # -- Shutdown
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_SHUTDOWN_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_SHUTDOWN_GUEST
                                    else:
                                        self.logger.error("Shutdown Action:The state can not run. - %d" % _v.status())
                                
                                elif status == GUEST_ACTION_DESTROY:
                                    cmdname = ["Destroy Guest", "Destroy guest"]
                                    if _v.is_destroyable() is True:
                                        # -- destroy
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_DESTROY_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_DESTROY_GUEST
                                    else:
                                        self.logger.error("Destroy Action:The state can not run. - %d" % _v.status())
                                        
                                elif status == GUEST_ACTION_SUSPEND:
                                    cmdname = ["Suspend Guest", "suspend guest"]
                                    if _v.is_suspendable() is True:
                                        # -- Suspend
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_SUSPEND_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_SUSPEND_GUEST
                                    else:
                                        self.logger.error("Destroy Action:The state can not run. - %d" % _v.status())
                                        
                                elif status == GUEST_ACTION_RESUME:
                                    cmdname = ["Resume Guest", "resume guest"]
                                    if _v.is_resumable() is True:
                                        # -- Resume
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_RESUME_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_RESUME_GUEST
                                    else:
                                        self.logger.error("Resume Action:The state can not run. - %d" % _v.status())
                    
                                elif status == GUEST_ACTION_REBOOT:
                                    cmdname = ["Reboot Guest", "reboot guest"]
                                    if _v.is_shutdownable() is True:
                                        # -- Reboot
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_REBOOT_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_REBOOT_GUEST
                                    else:
                                        self.logger.error("Reboot Action:The state can not run. - %d" % _v.status())
                    
                                elif status == GUEST_ACTION_ENABLE_AUTOSTART:
                                    opts["enable"] = None
                                    cmdname = ["Enable Autostart Guest", "enable autostart guest"]
                                    # -- Enable autostart guest
                                    _cmd = dict2command(
                                        "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_AUTOSTART_GUEST),
                                        opts)
                                        
                                    self.view.status = VIRT_COMMAND_AUTOSTART_GUEST
                    
                                elif status == GUEST_ACTION_DISABLE_AUTOSTART:
                                    opts["disable"] = None
                                    cmdname = ["Disable Autostart Guest", "disable autostart guest"]
                                    # -- Disable autostart guest
                                    _cmd = dict2command(
                                        "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_AUTOSTART_GUEST),
                                        opts)
                                        
                                    self.view.status = VIRT_COMMAND_AUTOSTART_GUEST

                                else:
                                    self.logger.error("Action:Bad Request. - request status=%d" % status)
                                    return web.badrequest()

                                break

Example 174

Project: karesansui
Source File: uriguestby1status.py
View license
    @auth
    def _PUT(self, *param, **params):
        """<comment-ja>
        ステータス更新
         - param
           - create = 0
           - shutdown = 1
           - destroy = 2
           - suspend = 3
           - resume = 4
           - reboot = 5
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """

        host_id =  param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id =  param[1]
        if uri_id is None:
            return web.notfound()

        if not validates_uriguest_status(self):
            return web.badrequest(self.view.alert)

        status = int(self.input.status)

        model = findbyhost1(self.orm, host_id)

        if model.attribute == 2:
            info = {}
            segs = uri_split(model.hostname)
            uri = uri_join(segs, without_auth=True)
            creds = ''
            if segs["user"] is not None:
                creds += segs["user"]
                if segs["passwd"] is not None:
                    creds += ':' + segs["passwd"]
            self.kvc = KaresansuiVirtConnectionAuth(uri,creds)

            try:
                host = MergeHost(self.kvc, model)
                for guest in host.guests:
                    _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                    if 0 < len(_virt):
                        for _v in _virt:
                            info = _v.get_info()
                            #uri = _v._conn.getURI()
                            if info["uuid"] == uri_id or (uri[0:5] == "test:"):

                                esc_name = "'%s'" % guest.info["model"].name
                                opts = {"name":esc_name,"connection":uri}

                                if creds != '':
                                    passwd_file = KARESANSUI_TMP_DIR + "/" + segs['host'] + ".auth"
                                    open(passwd_file, "w").write(creds)
                                    os.chmod(passwd_file, 0600)
                                    opts["passwd-file"] = passwd_file

                                if status == GUEST_ACTION_CREATE:
                                    # -- Create
                                    cmdname = ["Start Guest", "start guest"]
                                    if _v.is_creatable() is True:
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_START_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_START_GUEST
                                    else:
                                        self.logger.error("Create Action:The state can not run. - %d" % _v.status())
                                    
                                elif status == GUEST_ACTION_SHUTDOWN:
                                    cmdname = ["Shutdown Guest", "shutdown guest"]
                                    if _v.is_shutdownable() is True:
                                        # -- Shutdown
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_SHUTDOWN_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_SHUTDOWN_GUEST
                                    else:
                                        self.logger.error("Shutdown Action:The state can not run. - %d" % _v.status())
                                
                                elif status == GUEST_ACTION_DESTROY:
                                    cmdname = ["Destroy Guest", "Destroy guest"]
                                    if _v.is_destroyable() is True:
                                        # -- destroy
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_DESTROY_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_DESTROY_GUEST
                                    else:
                                        self.logger.error("Destroy Action:The state can not run. - %d" % _v.status())
                                        
                                elif status == GUEST_ACTION_SUSPEND:
                                    cmdname = ["Suspend Guest", "suspend guest"]
                                    if _v.is_suspendable() is True:
                                        # -- Suspend
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_SUSPEND_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_SUSPEND_GUEST
                                    else:
                                        self.logger.error("Destroy Action:The state can not run. - %d" % _v.status())
                                        
                                elif status == GUEST_ACTION_RESUME:
                                    cmdname = ["Resume Guest", "resume guest"]
                                    if _v.is_resumable() is True:
                                        # -- Resume
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_RESUME_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_RESUME_GUEST
                                    else:
                                        self.logger.error("Resume Action:The state can not run. - %d" % _v.status())
                    
                                elif status == GUEST_ACTION_REBOOT:
                                    cmdname = ["Reboot Guest", "reboot guest"]
                                    if _v.is_shutdownable() is True:
                                        # -- Reboot
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_REBOOT_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_REBOOT_GUEST
                                    else:
                                        self.logger.error("Reboot Action:The state can not run. - %d" % _v.status())
                    
                                elif status == GUEST_ACTION_ENABLE_AUTOSTART:
                                    opts["enable"] = None
                                    cmdname = ["Enable Autostart Guest", "enable autostart guest"]
                                    # -- Enable autostart guest
                                    _cmd = dict2command(
                                        "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_AUTOSTART_GUEST),
                                        opts)
                                        
                                    self.view.status = VIRT_COMMAND_AUTOSTART_GUEST
                    
                                elif status == GUEST_ACTION_DISABLE_AUTOSTART:
                                    opts["disable"] = None
                                    cmdname = ["Disable Autostart Guest", "disable autostart guest"]
                                    # -- Disable autostart guest
                                    _cmd = dict2command(
                                        "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_AUTOSTART_GUEST),
                                        opts)
                                        
                                    self.view.status = VIRT_COMMAND_AUTOSTART_GUEST

                                else:
                                    self.logger.error("Action:Bad Request. - request status=%d" % status)
                                    return web.badrequest()

                                break

Example 175

Project: bitmask_client
Source File: backend.py
View license
    def _init_txzmq(self):
        """
        Configure the txzmq components and connection.
        """
        self._zmq_factory = txzmq.ZmqFactory()
        self._zmq_factory.registerForShutdown()
        self._zmq_connection = txzmq.ZmqREPConnection(self._zmq_factory)

        context = self._zmq_factory.context
        socket = self._zmq_connection.socket

        def _gotMessage(messageId, messageParts):
            self._zmq_connection.reply(messageId, "OK")
            self._process_request(messageParts)

        self._zmq_connection.gotMessage = _gotMessage

        if flags.ZMQ_HAS_CURVE:
            # Start an authenticator for this context.
            auth = ThreadAuthenticator(context)
            auth.start()
            # XXX do not hardcode this here.
            auth.allow('127.0.0.1')

            # Tell authenticator to use the certificate in a directory
            auth.configure_curve(domain='*', location=zmq.auth.CURVE_ALLOW_ANY)
            public, secret = get_backend_certificates()
            socket.curve_publickey = public
            socket.curve_secretkey = secret
            socket.curve_server = True  # must come before bind

        proto, addr = self._server_address.split('://')  # tcp/ipc, ip/socket
        socket.bind(self._server_address)
        if proto == 'ipc':
            os.chmod(addr, 0600)

Example 176

Project: bitmask_client
Source File: signaler_qt.py
View license
    def _run(self):
        """
        Start a loop to process the ZMQ requests from the signaler client.
        """
        logger.debug("Running SignalerQt loop")
        context = zmq.Context()
        socket = context.socket(zmq.REP)

        if flags.ZMQ_HAS_CURVE:
            # Start an authenticator for this context.
            auth = ThreadAuthenticator(context)
            auth.start()
            auth.allow('127.0.0.1')

            # Tell authenticator to use the certificate in a directory
            auth.configure_curve(domain='*', location=zmq.auth.CURVE_ALLOW_ANY)
            public, secret = get_frontend_certificates()
            socket.curve_publickey = public
            socket.curve_secretkey = secret
            socket.curve_server = True  # must come before bind

        socket.bind(self.BIND_ADDR)

        if not flags.ZMQ_HAS_CURVE:
            os.chmod(self.SOCKET_FILE, 0600)

        while self._do_work.is_set():
            # Wait for next request from client
            try:
                request = socket.recv(zmq.NOBLOCK)
                # logger.debug("Received request: '{0}'".format(request))
                socket.send("OK")
                self._process_request(request)
            except zmq.ZMQError as e:
                if e.errno != zmq.EAGAIN:
                    raise
            time.sleep(0.01)

        logger.debug("SignalerQt thread stopped.")

Example 177

Project: automatron
Source File: actioning.py
View license
def execute_runbook(action, target, config, logger):
    ''' Execute action against target '''
    fabric.api.env = core.fab.set_env(config, fabric.api.env)
    fabric.api.env.host_string = target['ip']
    results = None
    if "plugin" in action['type']:
        plugin_file = action['plugin']
        plugin_file = '{0}/actions/{1}'.format(config['plugin_path'], plugin_file)
        dest_name = next(tempfile._get_candidate_names())
        destination = "{0}/{1}".format(config['actioning']['upload_path'], dest_name)
        with fabric.api.hide('output', 'running', 'warnings'):
            try:
                if action['execute_from'] == "ontarget":
                    logger.debug("Placing plugin script into {0}".format(destination))
                    fabric.api.put(plugin_file, destination)
                    fabric.api.run("chmod 700 {0}".format(destination))
                    cmd = "{0} {1}".format(destination, action['args'])
                    results = fabric.api.run(cmd)
                    fabric.api.run("rm {0}".format(destination))
                elif action['execute_from'] == "remote":
                    # Move file to temporary location and execute
                    shutil.copyfile(plugin_file, "/tmp/{0}".format(dest_name))
                    os.chmod("/tmp/{0}".format(dest_name), 0700)
                    cmd = "/tmp/{0} {1}".format(dest_name, action['args'])
                    results = fabric.api.local(cmd, capture=True)
                    os.remove("/tmp/{0}".format(dest_name))
                else:
                    logger.warn('Unknown "execute_from" specified in action')
                    return False
            except Exception as e:
                logger.debug("Could not execute plugin {0} for target {1}: {2}".format(
                    plugin_file, target['ip'], e.message))
    else:
        cmd = action['cmd']
        # Perform Check
        with fabric.api.hide('output', 'running', 'warnings'):
            try:
                if action['execute_from'] == "ontarget":
                    results = fabric.api.run(cmd)
                elif action['execute_from'] == "remote":
                    results = fabric.api.local(cmd, capture=True)
                else:
                    logger.warn('Unknown "execute_from" specified in action')
                    return False
            except Exception as e:
                logger.debug("Could not execute command {0}".format(cmd))
    if results:
        if results.succeeded is True:
            return True
        else:
            return False
    else:
        return False

Example 178

Project: automatron
Source File: actioning.py
View license
def execute_runbook(action, target, config, logger):
    ''' Execute action against target '''
    fabric.api.env = core.fab.set_env(config, fabric.api.env)
    fabric.api.env.host_string = target['ip']
    results = None
    if "plugin" in action['type']:
        plugin_file = action['plugin']
        plugin_file = '{0}/actions/{1}'.format(config['plugin_path'], plugin_file)
        dest_name = next(tempfile._get_candidate_names())
        destination = "{0}/{1}".format(config['actioning']['upload_path'], dest_name)
        with fabric.api.hide('output', 'running', 'warnings'):
            try:
                if action['execute_from'] == "ontarget":
                    logger.debug("Placing plugin script into {0}".format(destination))
                    fabric.api.put(plugin_file, destination)
                    fabric.api.run("chmod 700 {0}".format(destination))
                    cmd = "{0} {1}".format(destination, action['args'])
                    results = fabric.api.run(cmd)
                    fabric.api.run("rm {0}".format(destination))
                elif action['execute_from'] == "remote":
                    # Move file to temporary location and execute
                    shutil.copyfile(plugin_file, "/tmp/{0}".format(dest_name))
                    os.chmod("/tmp/{0}".format(dest_name), 0700)
                    cmd = "/tmp/{0} {1}".format(dest_name, action['args'])
                    results = fabric.api.local(cmd, capture=True)
                    os.remove("/tmp/{0}".format(dest_name))
                else:
                    logger.warn('Unknown "execute_from" specified in action')
                    return False
            except Exception as e:
                logger.debug("Could not execute plugin {0} for target {1}: {2}".format(
                    plugin_file, target['ip'], e.message))
    else:
        cmd = action['cmd']
        # Perform Check
        with fabric.api.hide('output', 'running', 'warnings'):
            try:
                if action['execute_from'] == "ontarget":
                    results = fabric.api.run(cmd)
                elif action['execute_from'] == "remote":
                    results = fabric.api.local(cmd, capture=True)
                else:
                    logger.warn('Unknown "execute_from" specified in action')
                    return False
            except Exception as e:
                logger.debug("Could not execute command {0}".format(cmd))
    if results:
        if results.succeeded is True:
            return True
        else:
            return False
    else:
        return False

Example 179

Project: stonix
Source File: configuration.py
View license
    def writeconfig(self, simpleconf, ruledata):
        """
        Writes current configuration data to the config file. If simpleconf is
        True we only write values that are changed or values that are marked
        as being in the simple configuration.

        @param bool simpleconf : Bool for whether or not the configuration file
        generated should be simple or full.
        @param dict: ruledata a dictionary of lists keyed by rulename and
        containing a packed list where index 0 is the rule help text and all
        remaining entries (if any) are configurationitem instances.
        @return  : void
        @author D. Kennel
        """
        confheader = """# STONIX.CONF
# STONIX configuration file
# This file is documented in the STONIX documentation. You may also review the
# documentation for this file with man stonix.conf.
[MAIN]
version = 100
"""
        conf = ''
        newline = '\n'
        conf = conf + confheader
        for rule in ruledata:
            sectionhead = '[' + rule + ']' + newline
            helptext = ruledata[rule][0]
            helptext = re.sub('^', '# ', helptext)
            helptext = re.sub('\\n', '\n# ', helptext)
            conf = conf + sectionhead
            conf = conf + helptext + newline
            for item in ruledata[rule]:
                try:
                    key = item.getkey()
                    value = item.getcurrvalue()
                    datatype = item.getdatatype()
                    defvalue = item.getdefvalue()
                    instruct = item.getinstructions()
                    instruct = re.sub('^', '# ', instruct)
                    instruct = re.sub('\\n', '\n# ', instruct)
                    usrcomment = item.getusercomment()
                    if datatype == 'list':
                        newval = ''
                        for element in value:
                            newval = newval + element + ' '
                        value = newval
                except(AttributeError):
                    continue
                uckey = 'UC' + key
                kvline = key + ' = ' + str(value) + newline
                instruct = instruct + newline
                usrcomment = usrcomment.replace("\n", r"\n")
                ucline = uckey + ' = ' + usrcomment + newline
                if not simpleconf:
                    conf = conf + instruct
                    conf = conf + kvline
                    conf = conf + ucline
                elif simpleconf and item.insimple():
                    conf = conf + instruct
                    conf = conf + kvline
                    conf = conf + ucline
                elif value != defvalue:
                    conf = conf + instruct
                    conf = conf + kvline
                    conf = conf + ucline
                elif usrcomment != '':
                    conf = conf + ucline
            conf = conf + newline
        try:
            fhandle = open(self.configpath, 'w')
            fhandle.write(conf)
            fhandle.close()
            os.chmod(self.configpath, 0644)
        except (IOError):
            print "FATAL ERROR: Could not write to config file"
            print "Check path to " + self.configpath
            sys.exit(1)

Example 180

Project: cstar_perf
Source File: client.py
View license
def create_credentials():
    """Create ecdsa keypair for authenticating with server. Save these to
    a config file in the home directory."""
    print("Config file is : {config_path}".format(config_path=CLIENT_CONFIG_PATH))
    # Check for cluster name:
    config = ConfigParser.RawConfigParser()
    config.read(CLIENT_CONFIG_PATH)
    if not config.has_section("cluster"):
        config.add_section("cluster")
    if config.has_option('cluster', 'name'):
        cluster_name = config.get('cluster', 'name')
    else:
        while True:
            cluster_name = raw_input('Enter a name for this cluster: ')
            if not re.match(r'^[a-zA-Z0-9_-]+$', cluster_name):
                print("Cluster name must be of the characters a-z, A-Z, 0-9, _ and -")
                continue
            break
        config.set('cluster', 'name', cluster_name)
        with open(CLIENT_CONFIG_PATH, "wb") as f:
            config.write(f)
        os.chmod(CLIENT_CONFIG_PATH, 0600)
    print("Cluster name is: {cluster_name}\n".format(cluster_name=cluster_name))

    # Check for existing client key:
    try:
        # Load existing config:
        apikey = APIKey.load()
    except BadConfigFileException:
        apikey = APIKey.new()
        apikey.save()

    print("Your public key is: {key}\n".format(key=apikey.get_pub_key()))

    # Check for existing server key:    
    try:
        # Load existing config:
        apikey = APIKey.load(key_type='server')
    except BadConfigFileException:
        server_key = raw_input("Input the server's public key:")
        try:
            apikey = APIKey(server_key)
        except:
            print("Invalid server key, does not decode to a valid ecdsa key.")
            exit(1)
        # Verify server key:
        verify_code = raw_input("Input the server verify code: ")
        token, sig = base64.decodestring(verify_code).split("|")
        apikey.verify_message(token, sig)
        apikey.save(key_type='server')
    
    print("Server public key is: {key}".format(key=apikey.get_pub_key()))

Example 181

Project: eavatar-me
Source File: config.py
View license
def save_conf(conf_file, content):
    with codecs.open(conf_file, 'wb', encoding='utf-8') as out:
        os.chmod(conf_file, 0600)

Example 182

Project: codejail
Source File: jail.py
View license
    def jail_code(self, code=None, files=None, extra_files=None, argv=None, stdin=None, slug=None):
        """
        Run code in the current jail.

        `code` is a string containing the code to run.  If no code is supplied,
        then the code to run must be in one of the `files` copied, and must be
        named in the `argv` list.

        `files` is a list of file paths, they are all copied to the jailed
        directory.  Note that no check is made here that the files don't contain
        sensitive information.  The caller must somehow determine whether to allow
        the code access to the files.  Symlinks will be copied as symlinks.  If the
        linked-to file is not accessible to the sandbox, the symlink will be
        unreadable as well.

        `extra_files` is a list of pairs, each pair is a filename and a bytestring
        of contents to write into that file.  These files will be created in the
        temp directory and cleaned up automatically.  No subdirectories are
        supported in the filename.

        `argv` is the command-line arguments to supply.

        `stdin` is a string, the data to provide as the stdin for the process.

        `slug` is an arbitrary string, a description that's meaningful to the
        caller, that will be used in log messages.

        Return a JailResult namedtuple with:

            .status: exit status of the process: an int, 0 for success
            .stdout: stdout of the program, a string
            .stderr: stderr of the program, a string

        """
        # We make a temp directory to serve as the home of the sandboxed code.
        # It has a writable "tmp" directory within it for temp files.
        with temp_directory() as homedir:

            # Make directory readable by other users ('sandbox' user needs to be
            # able to read it).
            os.chmod(homedir, 0775)

            # Make a subdir to use for temp files, world-writable so that the
            # sandbox user can write to it.
            tmptmp = os.path.join(homedir, "tmp")

Example 183

Project: museek-plus
Source File: utils.py
View license
def WriteLog(logfile, logsdir, fn, msg):
	if logfile is None:
		oldumask = os.umask(0077)

Example 184

Project: hubic-wrapper-to-swift
Source File: hubic.py
View license
    def __del__(self):
        if self.config_file:
            if options.verbose:
                print "-- Write config file back : %s " % self.config_file
            self.hubic_config = ConfigParser.RawConfigParser()

            self.hubic_config.add_section('hubic')
            if self.client_id:
                self.hubic_config.set('hubic', 'client_id', self.client_id)
            if self.client_secret:
                self.hubic_config.set('hubic', 'client_secret', self.client_secret)
            if self.redirect_uri:
                self.hubic_config.set('hubic', 'redirect_uri', self.redirect_uri)
            if self.username:
                self.hubic_config.set('hubic', 'username', self.username)
            if self.password:
                self.hubic_config.set('hubic', 'password', self.password)
            if self.refresh_token:
                self.hubic_config.set('hubic', 'refresh_token', self.refresh_token)
            if self.access_token:
                self.hubic_config.set('hubic', 'access_token', self.access_token)
            if self.token_expire:
                self.hubic_config.set('hubic', 'token_expire', self.token_expire)

            self.hubic_config.add_section('openstack')
            if self.os_auth_token:
                self.hubic_config.set('openstack', 'os_auth_token', self.os_auth_token)
            if self.os_storage_url:
                self.hubic_config.set('openstack', 'os_storage_url', self.os_storage_url)
            if self.os_token_expire:
                self.hubic_config.set('openstack', 'os_token_expire', self.os_token_expire)

            with open(self.config_file, 'wb') as configfile:
                self.hubic_config.write(configfile)
            os.chmod(self.config_file, 0600)

Example 185

Project: pushkin
Source File: pushkin_cli.py
View license
def start():
    """Starts the server"""
    os.umask(022)

Example 186

Project: cerbero
Source File: debian.py
View license
    def prepare(self, tarname, tmpdir, packagedir, srcdir):
        changelog = self._deb_changelog()
        compat = COMPAT_TPL

        control, runtime_files = self._deb_control_runtime_and_files()

        if len(runtime_files) != 0 or isinstance(self.package, MetaPackage):
            self.package.has_runtime_package = True
        else:
            self.package.has_runtime_package = False

        if self.devel:
            control_devel, devel_files = self._deb_control_devel_and_files()
        else:
            control_devel, devel_files = '', ''

        if len(devel_files) != 0 or isinstance(self.package, MetaPackage):
            self.package.has_devel_package = True
        else:
            self.package.has_devel_package = False

        copyright = self._deb_copyright()

        rules = self._deb_rules()
        source_format = SOURCE_FORMAT_TPL

        self._write_debian_file(packagedir, 'changelog', changelog)
        self._write_debian_file(packagedir, 'compat', compat)
        self._write_debian_file(packagedir, 'control', control + control_devel)
        self._write_debian_file(packagedir, 'copyright', copyright)
        rules_path = self._write_debian_file(packagedir, 'rules', rules)
        os.chmod(rules_path, 0755)

Example 187

Project: xos
Source File: ansible.py
View license
def run_template_ssh(name, opts, path='', expected_num=None):
    instance_name = opts["instance_name"]
    hostname = opts["hostname"]
    private_key = opts["private_key"]
    baremetal_ssh = opts.get("baremetal_ssh",False)
    if baremetal_ssh:
        # no instance_id or ssh_ip for baremetal
        # we never proxy to baremetal
        proxy_ssh = False
    else:
        instance_id = opts["instance_id"]
        ssh_ip = opts["ssh_ip"]
        try:
            proxy_ssh = Config().observer_proxy_ssh
        except:
            proxy_ssh = True

    (opts, fqp) = get_playbook_fn(opts, path)
    private_key_pathname = fqp + ".key"
    config_pathname = fqp + ".config"
    hosts_pathname = fqp + ".hosts"

    f = open(private_key_pathname, "w")
    f.write(private_key)
    f.close()

    f = open(config_pathname, "w")
    f.write("[ssh_connection]\n")
    if proxy_ssh:
        proxy_ssh_key = getattr(Config(), "observer_proxy_ssh_key", None)
        proxy_ssh_user = getattr(Config(), "observer_proxy_ssh_user", "root")
        if proxy_ssh_key:
            # If proxy_ssh_key is known, then we can proxy into the compute
            # node without needing to have the OpenCloud sshd machinery in
            # place.
            proxy_command = "ProxyCommand ssh -q -i %s -o StrictHostKeyChecking=no %[email protected]%s nc %s 22" % (proxy_ssh_key, proxy_ssh_user, hostname, ssh_ip)
        else:
            proxy_command = "ProxyCommand ssh -q -i %s -o StrictHostKeyChecking=no %[email protected]%s" % (private_key_pathname, instance_id, hostname)
        f.write('ssh_args = -o "%s"\n' % proxy_command)
    f.write('scp_if_ssh = True\n')
    f.write('pipelining = True\n')
    f.write('\n[defaults]\n')
    f.write('host_key_checking = False\n')
    f.write('timeout = 30\n')
    f.close()

    f = open(hosts_pathname, "w")
    f.write("[%s]\n" % instance_name)
    if proxy_ssh or baremetal_ssh:
        f.write("%s ansible_ssh_private_key_file=%s\n" % (hostname, private_key_pathname))
    else:
        # acb: Login user is hardcoded, this is not great
        f.write("%s ansible_ssh_private_key_file=%s ansible_ssh_user=ubuntu\n" % (ssh_ip, private_key_pathname))
    f.close()

    # SSH will complain if private key is world or group readable
    os.chmod(private_key_pathname, 0600)

Example 188

Project: enigma2
Source File: SoftcamPanel.py
View license
	def createInitdscript(self, camname, emubin, start, stop, wait=None):
		Adir = "/etc/init.d/softcam." + camname
		softcamfile = []
		softcamfile.append('#!/bin/sh')
		softcamfile.append('DAEMON=%s' % emubin)
		softcamfile.append('STARTCAM="%s"' % start)
		softcamfile.append('STOPCAM="%s"' % stop)
		softcamfile.append('DESC="Softcam"')
		softcamfile.append('')
		softcamfile.append('test -f $DAEMON || exit 0')
		softcamfile.append('set -e')
		softcamfile.append('')
		softcamfile.append('case "$1" in')
		softcamfile.append('	start)')
		softcamfile.append('		echo -n "starting $DESC: $DAEMON... "')
		if wait:
			softcamfile.append('		sleep ' + wait)
		softcamfile.append('		$STARTCAM')
		softcamfile.append('		echo "done."')
		softcamfile.append('		;;')
		softcamfile.append('	stop)')
		softcamfile.append('		echo -n "stopping $DESC: $DAEMON... "')
		softcamfile.append('		$STOPCAM')
		softcamfile.append('		echo "done."')
		softcamfile.append('		;;')
		softcamfile.append('	restart)')
		softcamfile.append('		echo "restarting $DESC: $DAEMON... "')
		softcamfile.append('		$0 stop')
		softcamfile.append('		echo "wait..."')
		softcamfile.append('		sleep 5')
		softcamfile.append('		$0 start')
		softcamfile.append('		echo "done."')
		softcamfile.append('		;;')
		softcamfile.append('	*)')
		softcamfile.append('		echo "Usage: $0 {start|stop|restart}"')
		softcamfile.append('		exit 1')
		softcamfile.append('		;;')
		softcamfile.append('esac')
		softcamfile.append('')
		softcamfile.append('exit 0')

		f = open( Adir, "w" )
		for x in softcamfile:
			f.writelines(x + '\n')
		f.close()

		self.container = eConsoleAppContainer()
		# Set execute rights
		os.chmod(Adir,0755)

Example 189

Project: bgperf
Source File: tester.py
View license
    def run(self, conf, brname=''):
        super(Tester, self).run(brname)

        startup = ['''#!/bin/bash
ulimit -n 65536''']

        peers = conf['tester'].values()

        for p in peers:
            with open('{0}/{1}.conf'.format(self.host_dir, p['router-id']), 'w') as f:
                local_address = p['local-address'].split('/')[0]
                config = '''neighbor {0} {{
    peer-as {1};
    router-id {2};
    local-address {3};
    local-as {4};
    static {{
'''.format(conf['target']['local-address'].split('/')[0], conf['target']['as'],
               p['router-id'], local_address, p['as'])
                f.write(config)
                for path in p['paths']:
                    f.write('      route {0} next-hop {1};\n'.format(path, local_address))
                f.write('''   }
}''')
            startup.append('''env exabgp.log.destination={0}/{1}.log \
exabgp.daemon.daemonize=true \
exabgp.daemon.user=root \
exabgp {0}/{1}.conf'''.format(self.guest_dir, p['router-id']))

        for p in peers:
            startup.append('ip a add {0} dev eth1'.format(p['local-address']))

        filename = '{0}/start.sh'.format(self.host_dir)
        with open(filename, 'w') as f:
            f.write('\n'.join(startup))
        os.chmod(filename, 0777)

Example 190

View license
    def test_user_umask(self):
        os.umask(007)

Example 191

Project: raspberry_pwn
Source File: topip.py
View license
def main():

    ######################################################################
    ## Parse the options, arguments, etc.
    ######################################################################
    try:
        optlist, args = getopt.getopt(sys.argv[1:], 'h?valqs:u:p:n:', ['help','h','?','ipv6','stddev='])
    except Exception, e:
        print str(e)
        exit_with_usage()
    options = dict(optlist)

    munin_flag = False
    if len(args) > 0:
        if args[0] == 'config':
            print 'graph_title Netstat Connections per IP'
            print 'graph_vlabel Socket connections per IP'
            print 'connections_max.label max'
            print 'connections_max.info Maximum number of connections per IP'
            print 'connections_avg.label avg'
            print 'connections_avg.info Average number of connections per IP'
            print 'connections_stddev.label stddev'
            print 'connections_stddev.info Standard deviation'
            return 0
        elif args[0] != '':
            print args, len(args)
            return 0
            exit_with_usage()
    if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
        print 'Help:'
        exit_with_usage()
    if '-s' in options:
        hostname = options['-s']
    else:
        # if host was not specified then assume localhost munin plugin.
        munin_flag = True
        hostname = 'localhost'
    # If localhost then don't ask for username/password.
    if hostname != 'localhost' and hostname != '127.0.0.1':
        if '-u' in options:
            username = options['-u']
        else:
            username = raw_input('username: ')
        if '-p' in options:
            password = options['-p']
        else:
            password = getpass.getpass('password: ')
    else:
        use_localhost = True

    if '-l' in options:
        log_flag = True
    else:
        log_flag = False
    if '-n' in options:
        average_n = int(options['-n'])
    else:
        average_n = None
    if '-v' in options:
        verbose = True
    else:
        verbose = False
    if '-a' in options:
        alert_flag = True
        (alert_addr_from, alert_addr_to) = tuple(options['-a'].split(','))
    else:
        alert_flag = False
    if '--ipv6' in options:
        ipv6_flag = True
    else:
        ipv6_flag = False
    if '--stddev' in options:
        stddev_trigger = float(options['--stddev'])
    else:
        stddev_trigger = 5

    if ipv6_flag:
        netstat_pattern = '(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+::ffff:(\S+):(\S+)\s+.*?\r'
    else:
        netstat_pattern = '(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(?:::ffff:)*(\S+):(\S+)\s+.*?\r'
        #netstat_pattern = '(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+):(\S+)\s+.*?\r'

    # run netstat (either locally or via SSH).
    if use_localhost:
        p = pexpect.spawn('netstat -n -t')
        PROMPT = pexpect.TIMEOUT
    else:
        p = pxssh.pxssh()
        p.login(hostname, username, password)
        p.sendline('netstat -n -t')
        PROMPT = p.PROMPT

    # loop through each matching netstat_pattern and put the ip address in the list.
    ip_list = {}
    try:
        while 1:
            i = p.expect([PROMPT, netstat_pattern])
            if i == 0:
                break
            k = p.match.groups()[4]
            if k in ip_list:
                ip_list[k] = ip_list[k] + 1
            else:
                ip_list[k] = 1
    except:
        pass

    # remove a few common, uninteresting addresses from the dictionary.
    ip_list = dict([ (key,value) for key,value in ip_list.items() if '192.168.' not in key])
    ip_list = dict([ (key,value) for key,value in ip_list.items() if '127.0.0.1' not in key])

    # sort dict by value (count)
    #ip_list = sorted(ip_list.iteritems(),lambda x,y:cmp(x[1], y[1]),reverse=True)
    ip_list = ip_list.items()
    if len(ip_list) < 1:
        if verbose: print 'Warning: no networks connections worth looking at.'
        return 0
    ip_list.sort(lambda x,y:cmp(y[1],x[1]))

    # generate some stats for the ip addresses found.
    if average_n <= 1:
        average_n = None
    s = stats(zip(*ip_list[0:average_n])[1]) # The * unary operator treats the list elements as arguments 
    s['maxip'] = ip_list[0]

    # print munin-style or verbose results for the stats.
    if munin_flag:
        print 'connections_max.value', s['max']
        print 'connections_avg.value', s['avg']
        print 'connections_stddev.value', s['stddev']
        return 0
    if verbose:
        pprint (s)
        print
        pprint (ip_list[0:average_n])

    # load the stats from the last run.
    try:
        last_stats = pickle.load(file(TOPIP_LAST_RUN_STATS))
    except:
        last_stats = {'maxip':None}

    if s['maxip'][1] > (s['stddev'] * stddev_trigger) and s['maxip']==last_stats['maxip']:
        if verbose: print 'The maxip has been above trigger for two consecutive samples.'
        if alert_flag:
            if verbose: print 'SENDING ALERT EMAIL'
            send_alert(str(s), 'ALERT on %s' % hostname, alert_addr_from, alert_addr_to)
        if log_flag:
            if verbose: print 'LOGGING THIS EVENT'
            fout = file(TOPIP_LOG_FILE,'a')
            #dts = time.strftime('%Y:%m:%d:%H:%M:%S', time.localtime())
            dts = time.asctime()
            fout.write ('%s - %d connections from %s\n' % (dts,s['maxip'][1],str(s['maxip'][0])))
            fout.close()

    # save state to TOPIP_LAST_RUN_STATS
    try:
        pickle.dump(s, file(TOPIP_LAST_RUN_STATS,'w'))
        os.chmod (TOPIP_LAST_RUN_STATS, 0664)
    except:
        pass

Example 192

Project: gkno_launcher
Source File: parameterSets.py
View license
  def export(self, graph, superpipeline, args, arguments):
    pipeline = superpipeline.pipeline

    # Get the parameter set name.
    setName = graph.exportParameterSet

    # Get the configuration file information for the pipeline and the available parameter sets.
    pipelineConfigurationData = superpipeline.pipelineConfigurationData[pipeline]
    sets                      = pipelineConfigurationData.parameterSets.sets

    # Check that the paramter set name is not already defined for the pipeline.
    if setName in sets: self.errors.exportToDefinedSet(pipeline, setName)

    # Define the name of the configuration file that holds the parameter set information.
    filename = str(pipeline + '-parameter-sets.json')

    # Open the configuration file for writing.
    filehandle = fh.fileHandling.openFileForWriting(filename)

    # Add the new parameter set information to the parameterSetAttributes.
    attributes             = parameterSetData()
    attributes.description = 'User specified parameter set.'
    attributes.id          = setName
    attributes.isExternal  = True
    sets[setName]          = attributes

    # First, loop over all file nodes and find those with values.
    self.counter      = 1
    observedArguments = []
    for nodeId in graph.getNodes('file'):
      values         = graph.getGraphNodeAttribute(nodeId, 'values')
      if len(values) > 0:

        # Check if this is a stub. If so, check if the stub has already been handled. If not, create
        # the attributes for the parameter set and mark the argument as handled. If so, ignore.
        stubExtension = graph.getGraphNodeAttribute(nodeId, 'stubExtension')
        if stubExtension:
          longFormArgument = graph.getGraphNodeAttribute(nodeId, 'longFormArgument')
          if longFormArgument not in observedArguments:
            observedArguments.append(longFormArgument)
            updatedNodeId = nodeId.rstrip('.' + stubExtension)
            updatedValues = [value.rstrip('.' + stubExtension) for value in values]
            attributes.data.append(self.getNodeAttributes(updatedNodeId, updatedValues))

        # If this is not a stub, handle the attribute.
        else: attributes.data.append(self.getNodeAttributes(nodeId, values))

    # Then loop over all options nodes.
    for nodeId in graph.getNodes('option'):
      values = graph.getGraphNodeAttribute(nodeId, 'values')
      if len(values) > 0: attributes.data.append(self.getNodeAttributes(nodeId, values))

    # Put all of the parameter set information in a dictionary that can be dumped to a json file.
    jsonParameterSets                  = OrderedDict()
    jsonParameterSets['parameter sets'] = []
    for parameterSet in sets:

      # Only include parameterSets that were marked as external.
      if sets[parameterSet].isExternal:
        parameterSetInformation                = OrderedDict()
        parameterSetInformation['id']          = parameterSet
        parameterSetInformation['description'] = sets[parameterSet].description
        parameterSetInformation['data']        = []

        # Set the nodes.
        for data in sets[parameterSet].data:
          nodeInformation             = OrderedDict()
          nodeInformation['id']       = data.id
          nodeInformation['node']     = data.nodeId
          nodeInformation['values']   = data.values
          parameterSetInformation['data'].append(nodeInformation)

        # Store this parameterSets data.
        jsonParameterSets['parameter sets'].append(parameterSetInformation)

    # Dump all the parameterSets to file.
    json.dump(jsonParameterSets, filehandle, indent = 2)
    filehandle.close()

    # Move the configuration file.
    os.chmod(filename, 0666)

Example 193

Project: gajim
Source File: check_paths.py
View license
def create_log_db():
	print _('creating logs database')
	con = sqlite.connect(logger.LOG_DB_PATH) 
	os.chmod(logger.LOG_DB_PATH, 0600) # rw only for us
	cur = con.cursor()
	# create the tables
	# kind can be
	# status, gcstatus, gc_msg, (we only recv for those 3),
	# single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent
	# to meet all our needs
	# logs.jid_id --> jids.jid_id but Sqlite doesn't do FK etc so it's done in python code
	# jids.jid text column will be JID if TC-related, room_jid if GC-related,
	# ROOM_JID/nick if pm-related.
	# also check optparser.py, which updates databases on gajim updates
	cur.executescript(
		'''
		CREATE TABLE jids(
			jid_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
			jid TEXT UNIQUE,
			type INTEGER
		);
		
		CREATE TABLE unread_messages(
			message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
			jid_id INTEGER
		);
		
		CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id);
		
		CREATE TABLE transports_cache (
			transport TEXT UNIQUE,
			type INTEGER
		);
		
		CREATE TABLE logs(
			log_line_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
			jid_id INTEGER,
			contact_name TEXT,
			time INTEGER,
			kind INTEGER,
			show INTEGER,
			message TEXT,
			subject TEXT
		);
		
		CREATE INDEX idx_logs_jid_id_kind ON logs (jid_id, kind);

		CREATE TABLE caps_cache (
			hash_method TEXT,
			hash TEXT,
			data BLOB);

		CREATE TABLE IF NOT EXISTS rooms_last_message_time(
			jid_id INTEGER PRIMARY KEY UNIQUE,
			time INTEGER
		);
		'''
		)

	con.commit()
	con.close()

Example 194

Project: golismero
Source File: test_wordlist.py
View license
    def test_file_permissions(self):
        _create_plugin_info()

        os.chmod(W_PATH, 0244)

Example 195

Project: grr
Source File: utils.py
View license
  def _GenerateZipInfo(self, arcname=None, compress_type=None, st=None):
    """Generate ZipInfo instance for the given name, compression and stat.

    Args:
      arcname: The name in the archive this should take.
      compress_type: Compression type (zipfile.ZIP_DEFLATED, or ZIP_STORED)
      st: An optional stat object to be used for setting headers.

    Returns:
      ZipInfo instance.

    Raises:
      ValueError: If arcname is not provided.
    """
    # Fake stat response.
    if st is None:
      st = os.stat_result((0100644, 0, 0, 0, 0, 0, 0, 0, 0, 0))

    mtime = time.localtime(st.st_mtime or time.time())
    date_time = mtime[0:6]
    # Create ZipInfo instance to store file information
    if arcname is None:
      raise ValueError("An arcname must be provided.")

    zinfo = zipfile.ZipInfo(arcname, date_time)
    zinfo.external_attr = (st[0] & 0xFFFF) << 16L  # Unix attributes

    if compress_type is None:
      zinfo.compress_type = self._compression
    else:
      zinfo.compress_type = compress_type

    zinfo.file_size = 0
    zinfo.compress_size = 0
    zinfo.flag_bits = 0x08  # Setting data descriptor flag.
    zinfo.CRC = 0x08074b50  # Predefined CRC for archives using data
    # descriptors.
    # This fills an empty Info-ZIP Unix extra field.
    zinfo.extra = struct.pack(
        "<HHIIHH",
        0x5855,
        12,
        0,  # time of last access (UTC/GMT)
        0,  # time of last modification (UTC/GMT)
        0,  # user ID
        0)  # group ID
    return zinfo

Example 196

View license
  def test_no_path_given_directory_exists(self):
    path1 = tempfile.mkdtemp()
    os.chmod(path1, 0777)

Example 197

Project: ahc
Source File: core_http.py
View license
	def __setup_standard(self, host_name, data, files):
		ext = {
			'python': 'py',
			'php': 'php',
			'ruby': 'rb'
		}
		if self.type in ext.keys():
			ext = ext[self.type]
			index_file = '%s/index.%s' % (data['website_dir'], ext)
		else:
			error_message('Type not supported!')
		self.project_root = data['website_dir']
		project = self.__get_project_name(host_name)
		venv = ""
		if self.base.options.venv:
			venv = self.__install_virtual_env(
				self.project_root, host_name, False
			)
			if self.base.options.module == 'apache':
				if self.base.options.wsgi:
					venv = ":%s" % venv
				else:
					putFile('%s/%s.py' % (self.project_root, project), """
activate_this = '%s/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from index import *
					""" % self.project_root)
		config = {
			'port': self.config['port'],
			'hostname': host_name,
			'root': data['website_dir'],
			'ssl_section': '',
			'venv': venv,
			'project': project,
			'optimize': self.__optimizationTemplate(),
			'socket_path': self.socket_path,
			'pid_path': self.pid_path,
			'basic_auth': self.__authTemplate(self.project_root)
		}
		if fileExists(files['host_file']):
			error_message('Host "%s" file is used!' % files['host_file'])
		if not putFile(files['host_file'], self.__getTemplate(self.type) % config):
			system_by_code("Error, when trying to save host file!")
		if int(self.base.main['use_ssl']) == 1:
			if fileExists(files['ssl_host_file']):
				error_message('Host file is used!')
			config['port'] = self.config['ssl_port']
			config['ssl_section'] = self.ssl_template
			if not putFile(files['ssl_host_file'], self.__getTemplate(self.type) % config):
				system_by_code("Error, when trying to save ssl host file!")
		if not putFile(index_file, self.__getTemplate(self.type, ext) % config):
			system_by_code("Error, when trying to save example file!")
		os.chmod(index_file, 0777)

Example 198

Project: check_mk
Source File: index.py
View license
def handler(req, profiling = True):
    req.content_type = "text/html; charset=UTF-8"
    req.header_sent = False

    # All URIs end in .py. We strip away the .py and get the
    # name of the page.
    req.myfile = req.uri.split("/")[-1][:-3]

    # Create an object that contains all data about the request and
    # helper functions for creating valid HTML. Parse URI and
    # store results in the request object for later usage.
    html = htmllib.html(req)
    html.id = {} # create unique ID for this request
    __builtin__.html = html
    req.uriinfo = htmllib.uriinfo(req)

    response_code = apache.OK
    try:
        # Do not parse variables again if profiling (and second run is done)
        if profiling:
            read_get_vars(req)
            read_cookies(req)

        # Ajax-Functions want no HTML output in case of an error but
        # just a plain server result code of 500
        fail_silently = html.has_var("_ajaxid")

        # Webservice functions may decide to get a normal result code
        # but a text with an error message in case of an error
        plain_error = html.has_var("_plain_error")

        config.load_config() # load multisite.mk
        if html.var("debug"): # Debug flag may be set via URL
            config.debug = True
        html.set_buffering(config.buffered_http_stream)

        # profiling can be enabled in multisite.mk
        if profiling and config.profile:
            import cProfile # , pstats, sys, StringIO, tempfile
            # the profiler looses the memory about all modules. We need to park
            # the request object in the apache module. This seems to be persistent.
            # Ubuntu: install python-profiler when using this feature
            apache._profiling_req = req
            profilefile = defaults.var_dir + "/web/multisite.profile"
            retcode = cProfile.run("import index; from mod_python import apache; index.handler(apache._profiling_req, False)", profilefile)
            file(profilefile + ".py", "w").write("#!/usr/bin/python\nimport pstats\nstats = pstats.Stats(%r)\nstats.sort_stats('time').print_stats()\n" % profilefile)
            os.chmod(profilefile + ".py", 0755)
            return apache.OK

        # Make sure all plugins are avaiable as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        load_all_plugins()

        # Detect mobile devices
        if html.has_var("mobile"):
            html.mobile = not not html.var("mobile")
        else:
            user_agent = html.req.headers_in['User-Agent']
            html.mobile = mobile.is_mobile(user_agent)

        # Redirect to mobile GUI if we are a mobile device and
        # the URL is /
        if req.myfile == "index" and html.mobile:
            req.myfile = "mobile"

        # Get page handler
        handler = pagehandlers.get(req.myfile, page_not_found)

        # Special handling for automation.py. Sorry, this must be hardcoded
        # here. Automation calls bybass the normal authentication stuff
        if req.myfile == "automation":
            try:
                handler()
            except Exception, e:
                html.write(str(e))
            return apache.OK

        # Prepare output format
        output_format = html.var("output_format", "html")
        html.set_output_format(output_format)

        # Is the user set by the webserver? otherwise use the cookie based auth
        if not req.user or type(req.user) != str:
            config.auth_type = 'cookie'
            # When not authed tell the browser to ask for the password
            req.user = login.check_auth()
            if req.user == '':
                if fail_silently:
                    # While api call don't show the login dialog
                    raise MKUnauthenticatedException(_('You are not authenticated.'))

                # Initialize the i18n for the login dialog. This might be overridden
                # later after user login
                load_language(html.var("lang", config.get_language()))

                # After auth check the regular page can be shown
                result = login.page_login(plain_error)
                if type(result) == tuple:
                    # This is the redirect to the requested page directly after successful login
                    req.user = result[0]
                    req.uri  = result[1]
                    req.myfile = req.uri.split("/")[-1][:-3]
                    handler = pagehandlers.get(req.myfile, page_not_found)
                else:
                    return result

        # Set all permissions, read site config, and similar stuff
        config.login(html.req.user)

        # Initialize the multiste i18n. This will be replaced by
        # language settings stored in the user profile after the user
        # has been initialized
        load_language(html.var("lang", config.get_language()))

        # All plugins might have to be reloaded due to a language change
        load_all_plugins()

        import default_permissions

        # User allowed to login at all?
        if not config.may("general.use"):
            reason = _("You are not authorized to use Check_MK Multisite. Sorry. "
                       "You are logged in as <b>%s</b>.") % config.user_id
            if len(config.user_role_ids):
                reason += _("Your roles are <b>%s</b>. " % ", ".join(config.user_role_ids))
            else:
                reason += _("<b>You do not have any roles.</b> ")
            reason += _("If you think this is an error, "
                        "please ask your administrator to check the permissions configuration.")

            if config.auth_type == 'cookie':
                reason += _('<p>You have been logged out. Please reload the page to re-authenticate.</p>')
                login.del_auth_cookie()

            raise MKAuthException(reason)

        # General access allowed. Now connect to livestatus
        connect_to_livestatus(html)

        handler()

    except MKUserError, e:
        if plain_error:
            html.write(_("User error") + ": %s\n" % e)
        elif not fail_silently:
            html.header("Invalid User Input")
            html.show_error(str(e))
            html.footer()

    except MKAuthException, e:
        if plain_error:
            html.write(_("Authentication error") + ": %s\n" % e)
        elif not fail_silently:
            html.header(_("Permission denied"))
            html.show_error(str(e))
            html.footer()

    except MKUnauthenticatedException, e:
        if plain_error:
            html.write(_("Missing authentication credentials") + ": %s\n" % e)
        elif not fail_silently:
            html.header(_("Not authenticated"))
            html.show_error(str(e))
            html.footer()
        response_code = apache.HTTP_UNAUTHORIZED

    except MKConfigError, e:
        if plain_error:
            html.write(_("Configuration error") + ": %s\n" % e)
        elif not fail_silently:
            html.header(_("Configuration Error"))
            html.show_error(str(e))
            html.footer()
        apache.log_error(_("Configuration error: %s") % (e,), apache.APLOG_ERR)

    except MKGeneralException, e:
        if plain_error:
            html.write(_("General error") + ": %s\n" % e)
        elif not fail_silently:
            html.header(_("Error"))
            html.show_error(str(e))
            html.footer()
        apache.log_error(_("Error: %s") % (e,), apache.APLOG_ERR)

    except livestatus.MKLivestatusNotFoundError, e:
        if plain_error:
            html.write(_("Livestatus-data not found") + ": %s\n" % e)
        elif not fail_silently:
            html.header(_("Data not found"))
            html.show_error(_("The following query produced no output:\n<pre>\n%s</pre>\n") % \
                    e.query)
            html.footer()
        response_code = apache.HTTP_NOT_FOUND

    except livestatus.MKLivestatusException, e:
        if plain_error:
            html.write(_("Livestatus problem") + ": %s\n" % e)
        elif not fail_silently:
            html.header(_("Livestatus problem"))
            html.show_error(_("Livestatus problem: %s") % e)
            html.footer()
        else:
            response_code = apache.HTTP_BAD_GATEWAY

    except apache.SERVER_RETURN:
        release_all_locks()
        html.live = None
        raise

    except Exception, e:
        if plain_error:
            html.write(_("Internal error") + ": %s\n" % e)
        elif not fail_silently:
            html.header(_("Internal error"))
            if config.debug:
                html.show_error("%s: %s<pre>%s</pre>" %
                    (_('Internal error') + ':', e, format_exception()))
            else:
                url = html.makeuri([("debug", "1")])
                html.show_error("%s: %s (<a href=\"%s\">%s</a>)" % (_('Internal error') + ':', e, url, _('Retry with debug mode')))
                apache.log_error("%s %s" % (_('Internal error') + ':', e), apache.APLOG_ERR)
            html.footer()
        response_code = apache.OK

    release_all_locks()
    html.live = None # disconnects from livestatus
    return response_code

Example 199

Project: meocloud-cli
Source File: locations.py
View license
def create_required_directories():
    create_required_directory(CONFIG_PATH)
    create_required_directory(UI_CONFIG_PATH)
    os.chmod(CONFIG_PATH, 0700)

Example 200

Project: easybuild-framework
Source File: filetools.py
View license
    def test_adjust_permissions(self):
        """Test adjust_permissions"""
        # set umask hard to run test reliably
        orig_umask = os.umask(0022)