Class: Steamy::App

Inherits:
Object
  • Object
show all
Defined in:
lib/steamy/app.rb

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, database = nil) ⇒ App

Returns a new instance of App.



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

def initialize(host = nil, database = nil)
    @host = host
    @database = database
    
    @user = ''
    @password = ''
  
    @sequel_pro = SequelPro.new()
    @connections = @sequel_pro.connections
end

Instance Method Details

#available_databasesObject



82
83
84
85
# File 'lib/steamy/app.rb', line 82

def available_databases
  setup_connection
  puts show_databases
end

#dumpObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/steamy/app.rb', line 15

def dump
    setup_connection
    
    if @database.nil?
      @database = get_database
    end
    
    timestamp = Time.now.strftime("%Y-%m-%d_%H%M%S")
    name = "#{@host}_#{@database}_#{timestamp}.sql.gz"
    remote_exec("'mysqldump #{@user} #{@password} #{@database} | gzip -c' > ~/Desktop/#{name}")
end

#get_databaseObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/steamy/app.rb', line 27

def get_database
  databases = show_databases.split("\n")
  
  puts "Choose a database to dump:"
  databases.each_with_index do |db, i|
    puts i.to_s + ': ' + db
  end

  # Get index from user
  index = gets.to_i
  databases[index]
end

#remote_exec(command) ⇒ Object



77
78
79
80
# File 'lib/steamy/app.rb', line 77

def remote_exec(command)
  command =  "ssh #{@host} #{command}"
  `#{command}`
end

#set_database(host, database) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/steamy/app.rb', line 62

def set_database(host, database)
  if host.nil? || @connections[@host]['database'].nil? || database
    database = database
  else
    puts "This user can only access '#{@connections[@host]['database']}', update credentials to access other databases or specify database to dump."
    exit 1
  end
  
  database
end

#setup_connectionObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/steamy/app.rb', line 40

def setup_connection
  if @host.nil? || @connections[@host].nil?
    if @host.nil?
      puts "no host, available hosts:"
    else 
      puts "no host #{@host}, available hosts:"
    end
    @sequel_pro.list
    exit 1
  end
  
  unless @connections[@host]['user'].empty?
    @user = "-u #{@connections[@host]['user']}"
  end
  
  unless @connections[@host]['password'].empty?
    @password = "-p\"#{@connections[@host]['password']}\""
  end

  @database = set_database(@host, @database)
end

#show_databasesObject



73
74
75
# File 'lib/steamy/app.rb', line 73

def show_databases
  remote_exec(sprintf('\'mysql -e "show databases" %s %s\'', @user, @password))
end