Module: HaveAPI::Fs

Defined in:
lib/haveapi/fs.rb,
lib/haveapi/fs/fs.rb,
lib/haveapi/fs/help.rb,
lib/haveapi/fs/main.rb,
lib/haveapi/fs/cache.rb,
lib/haveapi/fs/worker.rb,
lib/haveapi/fs/cleaner.rb,
lib/haveapi/fs/context.rb,
lib/haveapi/fs/factory.rb,
lib/haveapi/fs/version.rb,
lib/haveapi/fs/component.rb,
lib/haveapi/fs/hash_wrapper.rb,
lib/haveapi/fs/remote_control.rb,
lib/haveapi/fs/hash_list_wrapper.rb

Defined Under Namespace

Modules: Auth, Components, Help Classes: Cache, Cleaner, Component, Context, Factory, Fs, HashListWrapper, HashWrapper, RemoteControl, Worker

Constant Summary collapse

OPTIONS =

A list of accepted mount options

%i(api version auth_method user password token nodaemonize block
log index_limit)
USAGE =
<<END
    version=VERSION        API version to use
    auth_method=METHOD     Authentication method (basic, token, noauth)
    user                   Username
    password               Password
    token                  Authentication token
    nodaemonize            Stay in the foreground
    block                  Wait until blocking actions are finished
    log                    Enable logging while daemonized
    index_limit=LIMIT      Limit number of objects in resource directory
END
VERSION =
'0.9.0'

Class Method Summary collapse

Class Method Details

.auth_method(opts, default) ⇒ Object

Return authentication method based on mount options or return the default one.

Parameters:

  • opts (Hash)

    mount options

  • default (Symbol)

    name of the default authentication method



33
34
35
36
37
38
39
40
41
# File 'lib/haveapi/fs/main.rb', line 33

def self.auth_method(opts, default)
  return @auth_methods[opts[:auth_method].to_sym] if opts[:auth_method]

  @auth_methods.each_value do |m|
    return m if m.use?(opts)
  end

  default ? @auth_methods[default] : @auth_methods.values.first
end

.client(opts) ⇒ HaveAPI::Client::Client

Create and setup an instance of HaveAPI::Client::Client based on the mount options, calls self.daemonize if not configured otherwise.

Parameters:

  • opts (Hash)

    mount options

Returns:

  • (HaveAPI::Client::Client)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/haveapi/fs/main.rb', line 100

def self.client(opts)
  cfg = server_config(opts[:device])
  client = HaveAPI::Client::Client.new(
      opts[:device],
      version: opts[:version],
      identity: 'haveapi-fs',
      block: opts[:block] || false,
  )

  auth_klass = auth_method(opts, cfg && cfg[:last_auth])

  auth = auth_klass.new(
      (cfg && cfg[:auth][auth_klass.method_name]) || {},
      opts,
  )
  auth.validate
  auth.authenticate(client)

  # Fetch API description, must be done especially after authentication
  client.setup

  # Verify that authentication works
  auth.check(client)

  daemonize(opts) unless opts[:nodaemonize]
  client
end

.daemonize(opts) ⇒ Object

Perform a double-fork to make the process independent. Stdout and stderr are redirected either to a log file or to /dev/null.

Parameters:

  • opts (Hash)

    mount options



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/haveapi/fs/main.rb', line 69

def self.daemonize(opts)
  home = ::File.join(Dir.home, '.haveapi-fs', URI(opts[:device]).host)
  FileUtils.mkpath(home)
  
  pid = Process.fork

  if pid
    exit # Parent 1

  else
    pid = Process.fork
    exit if pid # Parent 2
  end

  # Only the child gets here
  STDIN.close
  
  f = File.open(
      opts[:log] ? File.join(home, 'haveapi-fs.log') : '/dev/null',
      'w'
  )

  STDOUT.reopen(f)
  STDERR.reopen(f)
end

.main(options = OPTIONS, usage = USAGE) ⇒ Object

Calls FuseFS.main with an instance of Fs.



129
130
131
132
133
134
135
# File 'lib/haveapi/fs/main.rb', line 129

def self.main(options = OPTIONS, usage = USAGE)
  FuseFS.main(ARGV, OPTIONS, USAGE, 'api_url') do |opts|
    fail "provide argument 'api_url'" unless opts[:device]

    HaveAPI::Fs.new(client(opts), opts)
  end
end

.new(*args) ⇒ Object



5
6
7
# File 'lib/haveapi/fs.rb', line 5

def self.new(*args)
  HaveAPI::Fs::Fs.new(*args)
end

.read_configHash?

Returns:

  • (Hash)

    if the config exists

  • (nil)

    if the config does not exist



45
46
47
48
49
50
51
52
53
54
# File 'lib/haveapi/fs/main.rb', line 45

def self.read_config
  config_path = "#{Dir.home}/.haveapi-client.yml"

  if File.exists?(config_path)
    YAML.load_file(config_path)

  else
    nil
  end
end

.register_auth(name, klass) ⇒ Object

Every authentication provider must register using this method.

Parameters:

  • name (Symbol)
  • klass (Class)


24
25
26
27
# File 'lib/haveapi/fs/main.rb', line 24

def self.register_auth(name, klass)
  @auth_methods ||= {}
  @auth_methods[name] = klass
end

.server_config(url) ⇒ Object

Return configuration of a particular server from the config hash.

Parameters:

  • url (String)

    URL of the API server



58
59
60
61
62
63
# File 'lib/haveapi/fs/main.rb', line 58

def self.server_config(url)
  cfg = read_config
  return nil if cfg.nil? || cfg[:servers].nil?

  cfg[:servers].detect { |s| s[:url] == url }
end