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/remote_control.rb

Defined Under Namespace

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

Constant Summary collapse

OPTIONS =

A list of accepted mount options

i(api version auth_method user password token nodaemonize log
index_limit)
USAGE =
"    version=VERSION        API version to use\n    auth_method=METHOD     Authentication method (basic, token, noauth)\n    user                   Username\n    password               Password\n    token                  Authentication token\n    nodaemonize            Stay in the foreground\n    log                    Enable logging while daemonized\n    index_limit=LIMIT      Limit number of objects in resource directory\n"
VERSION =
'0.1.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



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

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)


99
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
# File 'lib/haveapi/fs/main.rb', line 99

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

  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



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

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.



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

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



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

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)


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

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



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

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

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