class Mozzn::Commands::App

Public Instance Methods

appname() click to toggle source
# File lib/mozzn/commands/app.rb, line 231
def appname
  config_file_path = ".git/config"
  unless File.exists?(config_file_path)
    raise Thor::Error,"Unable to find a Git repository for this directory. You are probably not in the application directory or this application does not have a Git repository yet."
  end
  config = ParseConfig.new(config_file_path)
  our_key = 'remote "mozzn"'
  unless config.get_groups.include?(our_key)
    raise Thor::Error,"Unable to find a Git repository for this directory. You are probably not in the application directory or this application does not have a Git repository yet."
  end
  url = config['remote "mozzn"']['url']
  appname = url.split(':').last.split('.').first
end
console() click to toggle source
# File lib/mozzn/commands/app.rb, line 195
def console 
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  params = {
    name: appname
  }
  search_path = "applications/search"
  begin
    response = mozzn.get(search_path, params)
    if response.has_key?('info')
      raise Thor::Error, "#{response['info']}"
    else
      id = response['app_id']
      instances_path = "applications/#{id}/instances"
      response = mozzn.get(instances_path,nil)
      ip_address = response['instances'].first['data']['ip_address']
      if options[:command].present?
        system( "ssh app@#{ip_address} #{options[:command]}" )
      else
        system( "ssh app@#{ip_address}")
      end
      status = $?.exitstatus
    end
  rescue JSON::ParserError => e
    raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
  end
  rescue Mozzn::Disconnected
    say 'Unable to connect to Mozzn. Check your internet connection!', :red
  rescue Mozzn::UnexpectedOutput
    say 'UnexpectedOutput', :red
  end
create(name = nil) click to toggle source
# File lib/mozzn/commands/app.rb, line 9
def create name = nil
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  if name == nil
    raise Thor::Error, "You must enter application name."
  end
  path = 'applications'
  params = {
    application: {
      name: name
    }
  }
  response = mozzn.post(path, params)
  if response.has_key?('message')
    raise Thor::Error, "#{response['message']}."
  else
    say response['info'], :green
    git = Git.init
    unless File.exist?('.git')
        git.add(all: true)
      begin
        git.commit('First commit')
      rescue Git::GitExecuteError => e
        # Do nothing
      end
    end
    begin
      git.add_remote("mozzn", "git@git.mozzn.com:#{name}.git")
    rescue Git::GitExecuteError => e
      say 'Git remote already configured, skipping.'
    end
  end
rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn check the internet connection!', :red
rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end
destroy() click to toggle source
# File lib/mozzn/commands/app.rb, line 52
def destroy 
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  if options['appname'].present?
    name = options['appname']
  else
    name = appname
  end
  params = {
    name: name
  }
  search_path = "applications/search"
  begin
  response = mozzn.get(search_path, params)
  if response.has_key?('info')
    raise Thor::Error, "#{response['info']}."
  else
    id = response['app_id']
    resources_path = "applications/#{id}/remove"
    response = mozzn.get(resources_path,nil)
    say response['info'], :green 
  end  
  rescue JSON::ParserError => e
    raise Thor::Error,"You do not have an application with the name #{params[:name]}. Please check the application name."
  end
  rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
  rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end
list() click to toggle source
# File lib/mozzn/commands/app.rb, line 87
def list
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  path = "applications"
  response = mozzn.get(path, nil)
  if response.has_key? ('message')
    say response['message'], :yellow
    return
  end
  table = Terminal::Table.new(headings: ['Name', 'Status']) do |t|
    response['applications'].each do |app|
      key = app['name']
      value = app['status']
      t.add_row [key, value]  
    end
  end
  say "Your applications are:"
  say table
  rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
  rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red  
end
resources() click to toggle source
# File lib/mozzn/commands/app.rb, line 116
def resources 
  token = Mozzn::Config.new.read['token']
  if token.nil?
    raise Thor::Error,"You need to login in order to continue."
  end
  mozzn = Mozzn::Api.new(token)
  if options['appname'].present?
    name = options['appname']
  else
    name = appname
  end
  params = {
    name: name
  }
  search_path = "applications/search"
  begin
  response = mozzn.get(search_path, params)
  if response.has_key?('info')
    raise Thor::Error, "#{response['info']}"
  else
    id = response['app_id']
    resources_path = "applications/#{id}/resources"
    response = mozzn.get(resources_path,nil)
    if response.has_key?('info')
      raise Thor::Error, "#{response['info']}"
    else
      table1 = Terminal::Table.new(headings: ['Process', 'Command']) do |t|
        response['resources'].each do |resource|
          key = (resource.has_key?('command') ? resource['name'] : nil)
          value = (resource.has_key?('command') ? resource['command'] : nil)
          if key.present?
            t.add_row [key, value]
          end
        end
      end
      table2 = Terminal::Table.new(headings: ['Database', 'Role']) do |t|
        response['resources'].each do |resource|
          key = (resource.has_key?('role') ? resource['name'] : nil)
          value = (resource.has_key?('role') ? resource['role'] : nil)
          if key.present?
            t.add_row [key, value]
          end
        end
      end
    end
    instances_path = "applications/#{id}/instances"
    response = mozzn.get(instances_path,nil)
    if response.has_key?('info')
      raise Thor::Error, "#{response['info']}"
    else
      table3 = Terminal::Table.new(headings: ['Name', 'IP']) do |t|
        response['instances'].each do |instant|
          key = instant['data']['name']
          value = instant['data']['ip_address']
          if key.present?
            t.add_row [key, value]
          end
        end
      end
    end
    say "Processes:"
    say "#{table1}"
    say "Databases:"
    say "#{table2}"
    say "Instances:"
    say "#{table3}"  
  end

  rescue JSON::ParserError => e
    raise Thor::Error,"You do not have an application with the name #{params[:appname]}. Please check the application name."
  end
rescue Mozzn::Disconnected
  say 'Unable to connect to Mozzn. Check your internet connection!', :red
rescue Mozzn::UnexpectedOutput
  say 'UnexpectedOutput', :red
end