Class: Gleis::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/gleis/storage.rb

Overview

The class implements the methods required to the storage of a gleis app

Class Method Summary collapse

Class Method Details

.add(app_name, type) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/gleis/storage.rb', line 4

def self.add(app_name, type)
  token = Token.check
  body = API.request('post', 'storage', token, 'name': app_name, 'type': type)
  if body['success'] == 1
    puts "Successfully added #{type} storage to #{app_name}. As soon as you restart " \
      "your app the storage will be available under the #{body['mount_target']} directory."
  else
    puts "Failed to add storage: #{body['message']}"
  end
end

.attach(app_name, dir) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/gleis/storage.rb', line 15

def self.attach(app_name, dir)
  token = Token.check
  body = API.request('post', 'storage/attach', token, 'name': app_name, 'dir': dir)
  if body['success'] == 1
    puts "Successfully attached storage to #{dir} directory. As soon as you restart your app " \
      'the storage will be available under this directory.'
  else
    puts "Failed to attach storage: #{body['message']}"
  end
end

.list(app_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gleis/storage.rb', line 26

def self.list(app_name)
  token = Token.check
  action = 'storage/' + app_name
  body = API.request('get', action, token)
  puts "Available storage types:\n\n"
  printf("\t%-8s %s\n", 'TYPE', 'DESCRIPTION')
  printf("\t%-8s %s\n\n", '----', '-----------')
  body['data']['storage_types'].each do |st|
    printf("\t%-8s %s\n", st['name'], st['description'])
  end
  if body['data']['storage'].any?
    puts "\nStorage in use by app:\n\n"
    printf("\t%-8s %-30s %-25s %s\n", 'TYPE', 'MOUNT TARGET', 'CREATED ON', 'USAGE')
    printf("\t%-8s %-30s %-25s %s\n\n", '----', '------------', '-----------', '-----')
    body['data']['storage'].each do |s|
      printf("\t%-8s %-30s %-25s %s\n", s['storage_type_name'], s['mount_target'],
             Time.parse(s['created_at']).localtime.strftime('%c'), s['usage'])
    end
  else
    puts "\nYour app is not currently using any storage."
  end
end

.sync(app_name, dir, reverse = false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gleis/storage.rb', line 49

def self.sync(app_name, dir, reverse = false)
  token = Token.check
  abort("Directory #{dir} does not exist or is not a directory.") unless Dir.exist?(dir)
  # Get CLI parameters from API server
  params = Params.get_cli_parameters(token)
  abort('The rsync tool is not installed on this system.') unless Utils.which('rsync')
  body = API.request('get', "storage/#{app_name}", token)
  if body['success'] == 1
    if (storage = body['data']['storage'].first)
      ns_app_name = "#{body['data']['namespace']}_#{app_name}"
      ENV['RSYNC_PASSWORD'] = storage['password']
      directories = if reverse
                      "rsync://#{ns_app_name}@#{params['sync_server']}/#{ns_app_name} #{dir}/"
                    else
                      "#{dir}/ rsync://#{ns_app_name}@#{params['sync_server']}/#{ns_app_name}"
                    end
      exec("rsync -rth --progress #{directories}")
    else
      puts 'No storage configured yet.'
    end
  else
    puts 'Failed to get storage info.'
  end
end