<commit_before># Copyright (c) 2009-2011 VMware, Inc.
$:.unshift File.join(File.dirname(__FILE__), ".")
$:.unshift File.join( ENV['HOME'], 'stackato', 'vcap', 'fence', 'em-fence-client', 'lib' )

require "base/node"
require "uuidtools"
require "em/fence/client"

module VCAP
  module Services
    module Filesystem
      class Node < VCAP::Services::Base::Node
      end
    end
  end
end

require "filesystem_service/common"
require "filesystem_service/error"

class VCAP::Services::Filesystem::Node

  include VCAP::Services::Filesystem::Common
  include VCAP::Services::Filesystem

  def initialize(options)
    super(options)

    @available_capacity = options[:capacity]
    @base_dir = options[:base_dir]
    @max_fs_size = options[:max_fs_size]
    FileUtils.mkdir_p(@base_dir)
  end

  class ProvisionedService
    attr_accessor :name, :user, :private_key, :plan, :dir

    def initialize
      @name        = nil
      @user        = nil
      @private_key = nil
      @dir         = nil
      @plan        = nil
    end
  end

  def announcement
    @capacity_lock.synchronize do
      a = {
          :available_capacity => @capacity,
          :capacity_unit => capacity_unit
      }
    end
  end

  def gen_credentials(instance)
    credentials = {
      "hostname"    => @local_ip,
      "host"        => @local_ip,
      "dir"         => instance.dir,
      "user"        => instance.user,
      "private_key" => instance.private_key,
      "name"        => instance.name,
    }
  end

  def provision(plan, credentials=nil, db_file = nil)
    instance = ProvisionedService.new
    fence = EM::Fence::Client.new
    if credentials
      instance.name        = credentials["name"]
      instance.user        = credentials["user"]
      instance.dir         = credentials["dir"]
      instance.private_key = credentials["private_key"]
    else
      begin
        fs_instance = fence.create_filesystem_instance( :limit => @max_fs_size )
        # instance = {
        #   "instance_id" => 'u3h5ui245i24g5oi24g5',
        #   "dir"         => '/var/vcap/services/filesystem/storage/filesystem-u3h5...',
        #   "private_key" => '-----BEGIN RSA PRIVATE KEY...',
        # }
        raise FilesystemError.new(FilesystemError::FILESYSTEM_CREATE_INSTANCE_DIR_FAILED, name) if instance == nil

        instance.name        = fs_instance["instance_id"]
        instance.private_key = fs_instance["private_key"]
        instance.user        = fs_instance["user"]
        instance.dir         = fs_instance["dir"]
      rescue => e
        fence.cleanup_filesystem_instance( :service_id => instance.name )
        raise e
      end
    end

    gen_credentials(instance)
  end

  def unprovision(instance_id, credentials_list = [])
    @logger.info("unprovisioning instance: #{instance_id}")
    EM::Fence::Client.new.cleanup_filesystem_instance( :service_id => instance_id )
    {}
  end

  def get_instance(name)
    svc = ProvisionedService.new
    svc.name = name
    svc.user = "stackatofs-#{name}"
    svc.dir  = File.join(@base_dir, svc.user, "storage")

    raise FilesystemError.new(FilesystemError::FILESYSTEM_FIND_INSTANCE_FAILED, name) unless File.directory? svc.dir

    private_key = EM::Fence::Client.new.pull_private_key( :service_id => svc.name )
    raise FilesystemError.new(FilesystemError::FILESYSTEM_FIND_INSTANCE_FAILED, name) if private_key == ""
    svc.private_key = private_key

    svc
  end

  def bind(instance_id, binding_options = :all, credentials = nil)
    instance = nil
    if credentials
      instance = get_instance(credentials["name"])
    else
      instance = get_instance(instance_id)
    end
    gen_credentials(instance)
  end

  def unbind(credentials)
    {}
  end
end<commit_msg>Use blocking client instead of non-blocking for fs-service (services backend uses threads)<commit_after># Copyright (c) 2009-2011 VMware, Inc.
$:.unshift File.join(File.dirname(__FILE__), ".")
$:.unshift File.join( ENV['HOME'], 'stackato', 'vcap', 'fence', 'fence-client', 'lib' )

require "base/node"
require "uuidtools"
require "fence/client"

module VCAP
  module Services
    module Filesystem
      class Node < VCAP::Services::Base::Node
      end
    end
  end
end

require "filesystem_service/common"
require "filesystem_service/error"

class VCAP::Services::Filesystem::Node

  include VCAP::Services::Filesystem::Common
  include VCAP::Services::Filesystem

  def initialize(options)
    super(options)

    @available_capacity = options[:capacity]
    @base_dir = options[:base_dir]
    @max_fs_size = options[:max_fs_size]
    FileUtils.mkdir_p(@base_dir)
  end

  class ProvisionedService
    attr_accessor :name, :user, :private_key, :plan, :dir

    def initialize
      @name        = nil
      @user        = nil
      @private_key = nil
      @dir         = nil
      @plan        = nil
    end
  end

  def announcement
    @capacity_lock.synchronize do
      a = {
          :available_capacity => @capacity,
          :capacity_unit => capacity_unit
      }
    end
  end

  def gen_credentials(instance)
    credentials = {
      "hostname"    => @local_ip,
      "host"        => @local_ip,
      "dir"         => instance.dir,
      "user"        => instance.user,
      "private_key" => instance.private_key,
      "name"        => instance.name,
    }
  end

  def provision(plan, credentials=nil, db_file = nil)
    instance = ProvisionedService.new
    fence = Fence::Client.new
    if credentials
      instance.name        = credentials["name"]
      instance.user        = credentials["user"]
      instance.dir         = credentials["dir"]
      instance.private_key = credentials["private_key"]
    else
      begin
        fs_instance = fence.create_filesystem_instance( :limit => @max_fs_size )
        # instance = {
        #   "instance_id" => 'u3h5ui245i24g5oi24g5',
        #   "dir"         => '/var/vcap/services/filesystem/storage/filesystem-u3h5...',
        #   "private_key" => '-----BEGIN RSA PRIVATE KEY...',
        # }
        raise FilesystemError.new(FilesystemError::FILESYSTEM_CREATE_INSTANCE_DIR_FAILED, name) if instance == nil

        instance.name        = fs_instance["instance_id"]
        instance.private_key = fs_instance["private_key"]
        instance.user        = fs_instance["user"]
        instance.dir         = fs_instance["dir"]
      rescue => e
        fence.cleanup_filesystem_instance( :service_id => instance.name )
        raise e
      end
    end

    gen_credentials(instance)
  end

  def unprovision(instance_id, credentials_list = [])
    @logger.info("unprovisioning instance: #{instance_id}")
    Fence::Client.new.cleanup_filesystem_instance( :service_id => instance_id )
    {}
  end

  def get_instance(name)
    svc = ProvisionedService.new
    svc.name = name
    svc.user = "stackatofs-#{name}"
    svc.dir  = File.join(@base_dir, svc.user, "storage")

    raise FilesystemError.new(FilesystemError::FILESYSTEM_FIND_INSTANCE_FAILED, name) unless File.directory? svc.dir

    private_key = Fence::Client.new.pull_private_key( :service_id => svc.name )
    raise FilesystemError.new(FilesystemError::FILESYSTEM_FIND_INSTANCE_FAILED, name) if private_key == ""
    svc.private_key = private_key

    svc
  end

  def bind(instance_id, binding_options = :all, credentials = nil)
    instance = nil
    if credentials
      instance = get_instance(credentials["name"])
    else
      instance = get_instance(instance_id)
    end
    gen_credentials(instance)
  end

  def unbind(credentials)
    {}
  end
end
{'fraction_non_alphanumeric': 0.11389884508164078, 'fraction_numerical': 0.005575467941059339, 'mean_word_length': 2.828252032520325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1982169', 'n_tokens_mistral': 2393, 'n_tokens_neox': 2358, 'n_words': 466}