Class: CreateRailsMysqlDb::Application

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

Class Method Summary collapse

Class Method Details

.run!(*arguments) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/create_rails_mysql_db.rb', line 4

def run!(*arguments)

  require 'rubygems'
  require 'yaml'

  @DBPATH='config/database.yml'
  @DBTYPES=['mysql', 'mysql2']

  if arguments.include?('-h')
    puts  <<-eos
create_rails_mysql_db
Without options reads config/database.yml and create listed databases, grant access to username
-g : options to generate new password, and replace username to database name
          eos
    exit
  end

  generate_password=arguments.include?('-g')
  puts "Warning if password empty username=database name, new password will be generated\nconfig/database.yml will be overwrited\n" if (generate_password)

  begin

    def newpwd(len)
      chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
      newpass = ""
      1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
      return newpass
    end
    newpass = newpwd(8)
    changed=0

    File.open(@DBPATH) do |f|
      dbconf=YAML.load(f)
      dbconf.each do |type,db| 
        raise 'Adapter must be mysql' unless @DBTYPES.include?(db['adapter']) 
        raise 'Not defined username' unless db['username']
        if !db['password'] && generate_password
          dbconf[type]['password'] = newpass
          dbconf[type]['username'] = dbconf[type]['database'].length>16 ? dbconf[type]['database'].gsub(/(\_.{4}).*/, '\1') : dbconf[type]['database']
          raise 'Too long username' if dbconf[type]['username'].length>16
          changed+=1
        elsif !db['password']
          raise 'Not defined password' unless db['password']
        end
        raise 'Too long username' if dbconf[type]['username'].length>16
      end

      File.open(@DBPATH, "w") { |f| YAML::dump(dbconf, f) } if (changed>0)
      puts "Enter mysql root password:"
      STDOUT.flush
      system "stty -echo" 
      mysql_root=STDIN.gets.gsub("\n", "")
      system "stty echo" 
      dbconf.each do |type,db|
        mysql_command="mysql -uroot -p\"#{mysql_root}\" -e \"create database #{db['database']}; grant all on #{db['database']}.* to #{db['username']}@'localhost' identified by '#{db['password']}'\";"
        system(mysql_command)
      end
    end
  rescue Errno::ENOENT => e
    puts "Can't open #{@DBPATH}"
  rescue ArgumentError, SyntaxError => e
    puts "Error in yaml format"
  rescue => e
    puts e.message
  end
end