Class: Spider::MySQLSetupWizard

Inherits:
Wizard show all
Defined in:
lib/spiderfw/setup/spider_setup_wizard.rb

Instance Method Summary collapse

Methods inherited from Wizard

#add_key, #ask, #ask!, #ask?, #do_ask, #get_value, #implementation, #initialize, #set_value, #values, #wizard_instance

Constructor Details

This class inherits a constructor from Spider::Wizard

Instance Method Details

#discoveryObject



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 344

def discovery
    check = ['/tmp/mysql.sock', '/var/lib/mysql/mysql.sock', '/var/run/mysqld/mysqld.sock']
    local = []
    check.each do |c|
        local << {:socket => c} if File.socket?(c)
    end
    if local.empty?
        localhost = true
        begin
            s = TCPSocket::open('localhost', 3306)
            s.close
        rescue
            localhost = false
        end
        local << {:host => 'localhost', :port => 3306} if localhost
    end
    local
end

#get_urlObject



363
364
365
366
367
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 363

def get_url
    return nil unless @user && @pass && @db_name && ((@host && @port) || @socket)
    @host ||= 'localhost'
    "db:mysql://#{@user}:#{@pass}@#{@host}:#{(@port || @socket)}/#{@db_name}"
end

#parse_url(url) ⇒ Object



369
370
371
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 369

def parse_url(url)
    @host, @user, @pass, @db_name, @port, @socket = Spider::Model::Storage::Db::Mysql.parse_url(url)
end

#runObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/spiderfw/setup/spider_setup_wizard.rb', line 228

def run
    
    require 'rubygems'
    require 'rubygems/command.rb'
    require 'rubygems/dependency_installer.rb'
    unless Spider.gem_available?('mysql') || Spider.gem_available?('ruby-mysql')
        if ask? _("Mysql gem is not available. Install?")
            ok = false
            while !ok
                inst = Gem::DependencyInstaller.new
                begin
                    inst.install 'mysql'
                rescue
                    begin
                        inst.install 'ruby-mysql'
                    rescue
                        error _("Installation of mysql gem failed.")
                        return
                    end
                    puts _(
                    "The mysql gem failed to compile, so the pure-ruby version was installed.
                    You should install the compiled version manually for better performance.")
                end
            end
        else
            error _("Can't configure mysql without the mysql gem.")
            return
        end
    end
    require 'mysql'
    
    local = discovery
    use_local = nil
    unless local.empty?
        local_str = local.map{ |l|
            l[:socket] ? l[:socket] : "#{l[:host]}:#{l[:port]}"
        }
        use_local = ask _("We found the following MySQL instances. Do you want to use one of them?"), 
            :choices => local_str, :return_index => true, :allow_cancel => _("No")
        use_local = local[use_local] if use_local
    end
    @socket = nil
    @host = nil
    @port = nil
    if use_local
        @socket = use_local[:socket]
        @host = use_local[:host]
        @port = use_local[:port]
    else
        ok = false
        while !ok
            if (ask _("Do you want to connect via socket or via network?"), 
                    :choices => ['socket', 'network']) == 'network'
                ask _("Host:"), :host, :default => 'localhost'
                ask _("Port:"), :port, :default => 3306
            else
                ask _("Socket location"), :socket
            end
            if @socket
                if File.socket?(@socket)
                    ok = true
                else
                    error _("%s does not seem to be a socket") % @socket
                end
            else
                begin
                    s = TCPSocket::open(@host, @port)
                    ok = true
                rescue
                    error _("Connection to %s failed") % "#{@host}:#{@port}"
                    ok = ask_error
                end
            end
            
        end
    end
    ok = false
    connect_ok = false
    while !ok
        ask! _("Username"), :user
        ask _("Password"), :pass
        conn = @socket ? @socket : "#{@host}:#{@port}"
        notify_partial _("Trying to connect to mysql at %s ... ") % conn
        begin
            m = ::Mysql.new(@host, @user, @pass, nil, @port, @socket)
            m.ping
            m.close
            connect_ok = true
            ok = true
            notify_partial("Ok.", true)
        rescue
            notify_partial("", true)
            error _("Connection failed.")
            ok = ask_error
        end
    end
    ok = false
    while !ok
        ask _("Db name:"), :db_name
        if connect_ok
            notify_partial _("Checking if db exists... ")
            begin
                m = ::Mysql.new(@host, @user, @pass, @db_name, @port, @socket)
                ok = true
                notify_partial("Ok.", true)
            rescue => exc
                notify_partial("", true)
                error _("Database %s does not exist.") % @db_name
                ok = ask_error
            end
        else
            ok = true
        end
    end
end