class Gleis::Storage

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

Public Class Methods

add(app_name, type) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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