Class: Webbynode::Commands::Database

Inherits:
ActionCommand show all
Defined in:
lib/webbynode/commands/database.rb

Constant Summary

Constants inherited from Webbynode::Command

Webbynode::Command::Aliases, Webbynode::Command::CommandError, Webbynode::Command::InvalidCommand, Webbynode::Command::InvalidOption, Webbynode::Command::Settings

Instance Attribute Summary collapse

Attributes inherited from ActionCommand

#action

Instance Method Summary collapse

Methods inherited from ActionCommand

allowed_actions, #execute

Methods inherited from Webbynode::Command

add_alias, #api, class_for, command, command_class_name, description, for, #gemfile, #git, help, inherited, #initialize, #io, #no?, #notify, option, #option, #options, options_help, #param, #param_values, parameter, #params, #params_hash, params_help, #pushand, #remote_executor, requires_initialization!, requires_options!, requires_pushed_application!, #run, #server, setting, #settings, summary, summary_help, usage, #validate_initialization, #validate_options, #yes?

Constructor Details

This class inherits a constructor from Webbynode::Command

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



11
12
13
# File 'lib/webbynode/commands/database.rb', line 11

def db
  @db
end

Instance Method Details

#ask_db_credentials(force = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/webbynode/commands/database.rb', line 94

def ask_db_credentials(force=false)
  retrieve_db_credentials
  
  if force || db[:name].nil?
    db[:name] = query("Database name", db[:name] || io.db_name)
    db[:user] = query("    User name", db[:user] || io.db_name)
  end
  
  save_password = false
  if force || db[:password].nil?
    db[:password] = query("     Password", "")
    save_password = ask("Save password (y/n)? ").downcase == 'y'
  end
  
  save_db_credentials(save_password)
end

#configObject



25
26
27
# File 'lib/webbynode/commands/database.rb', line 25

def config
  ask_db_credentials(true)
end

#defaultObject



13
14
15
# File 'lib/webbynode/commands/database.rb', line 13

def default
  io.log "Missing action: use #{"pull".color(:yellow)}, #{"push".color(:yellow)} or #{"config".color(:yellow)}. For more help use #{"#{File.basename $0} help database".color(:yellow)}."
end

#go(action) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/webbynode/commands/database.rb', line 29

def go(action)
  ask_db_credentials
  
  io.log "#{action.to_s.capitalize}ing remote data to database #{db[:name]}"
  
  db_name  = pushand.remote_db_name
  password = remote_executor.retrieve_db_password
  ip       = git.parse_remote_ip
  
  if option(:debug)
    io.log ""
    io.log "Retrieving contents from #{db_name} database in #{ip}..."
  end
  
  taps = Webbynode::Taps.new(db_name, password, io, remote_executor)
  taps.debug = option(:debug)
  begin
    io.log "Checking for dependencies..." if option(:debug)
    taps.ensure_gems!

    io.log "Starting taps in server mode..." if option(:debug)
    taps.start

    io.log "Waiting for taps to start..." if option(:debug)
    sleep 4

    io.log "Sending action #{action} with db #{db[:name]}..." if option(:debug)
    taps.send(action, :user => db[:user], 
      :password     => db[:password],
      :database     => db[:name],
      :remote_ip    => ip)
  rescue TapsError
    if $!.message =~ /LoadError: no such file to load -- (.*)/
      io.log "#{"ERROR:".color(:red)} Missing database adapter. You need to install #{$1.color(:yellow)} gem to handle your database."
    elsif $!.message =~ /Mysql::Error: Unknown database '(.*)'/
      io.log "#{"ERROR:".color(:red)} Unknown database #{$1.color(:yellow)}. Create the local database and try again."
    elsif $!.message =~ /Sequel::DatabaseConnectionError -\> Mysql::Error: (.*)/
      io.log "#{"ERROR:".color(:red)} Invalid MySQL credentials for your local database (#{$1})"
    else
      if $!.message =~ /(.*) -\> (.*)/
        io.log "#{"ERROR:".color(:red)} Unexpected error - #{$2}"
      else
        io.log "#{"ERROR:".color(:red)} Unexpected error - #{$!.message}"
      end
    end
  ensure
    io.log "Stopping taps server..."
    taps.finish
  end    
end

#pullObject



17
18
19
# File 'lib/webbynode/commands/database.rb', line 17

def pull
  go :pull
end

#pushObject



21
22
23
# File 'lib/webbynode/commands/database.rb', line 21

def push
  go :push
end

#query(question, default) ⇒ Object



80
81
82
83
84
# File 'lib/webbynode/commands/database.rb', line 80

def query(question, default)
  answer = ask("#{question} [#{default}]: ")
  answer = default if answer.blank?
  answer
end

#retrieve_db_credentialsObject



86
87
88
89
90
91
92
# File 'lib/webbynode/commands/database.rb', line 86

def retrieve_db_credentials
  @db = {
    :name => io.load_setting("database_name"),
    :user => io.load_setting("database_user"),
    :password => io.load_setting("database_password")
  }
end

#save_db_credentials(save_password) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/webbynode/commands/database.rb', line 111

def save_db_credentials(save_password)
  io.add_setting "database_name", db[:name]
  io.add_setting "database_user", db[:user]
  
  if save_password
    io.add_setting "database_password", db[:password]
  end
end