Class: GitPusshuTen::Commands::Mysql

Inherits:
Base
  • Object
show all
Defined in:
lib/gitpusshuten/modules/mysql/command.rb

Instance Attribute Summary

Attributes inherited from Base

#cli, #command, #configuration, #environment, #hooks, #perform_hooks

Instance Method Summary collapse

Methods inherited from Base

#c, #command_object, description, #e, #error, example, #g, #git, #help, #local, long_description, #message, #perform!, #perform_hooks!, #post_perform!, #pre_perform!, #prompt_for_root_password!, #prompt_for_user_password!, #r, #requires_user_existence!, #silent, #standard, usage, #validate!, #warning, #y, #yes?

Constructor Details

#initialize(*objects) ⇒ Mysql

Returns a new instance of Mysql.



12
13
14
15
16
17
18
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 12

def initialize(*objects)
  super
  
  @command = cli.arguments.shift
  
  help if command.nil? or e.name.nil?
end

Instance Method Details

#confirm_access!Object

Confirms whether the provided password is correct



181
182
183
184
185
186
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 181

def confirm_access!
  if e.execute_as_root("mysqladmin -u root --password='#{@existing_password}' ping") =~ /Access denied for user/
    error "Incorrect password."
    exit
  end
end

#perform_add_user!Object

Adds the environment user to the database



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 85

def perform_add_user!
  if not e.installed?('mysql')
    error "MySQL isn't installed."
    exit
  end
  
  ##
  # Confirm Root Password
  message "Please provide your MySQL #{y('root')} password."
  @existing_password = ask('') { |q| q.echo = false }
  confirm_access!
  
  ##
  # Ask password for the new user
  message "Please provide a new password for your MySQL database user."
  prompt_for_new_password!
          
  command  = "\"CREATE USER '#{c.user}'@'localhost' IDENTIFIED BY '#{@new_password}';"
  command += "GRANT ALL ON *.* TO '#{c.user}'@'localhost';\""
  
  response = e.execute_as_root("mysql -u root --password='#{@existing_password}' -e #{command}")
  if not response =~ /ERROR 1396/
    message "User #{y(c.user)} has been added!"
  else
    error "User #{y(c.user)} already exists."
  end
  
end

#perform_change_root_password!Object

Changes the root password of the MySQL Server



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 45

def perform_change_root_password!
  if not e.installed?('mysql')
    error "MySQL isn't installed."
    exit
  end
  
  message "Please provide your MySQL #{y('root')} password."
  @existing_password = ask('') { |q| q.echo = false }
  confirm_access!
  
  message "Please provide a new password for your MySQL database #{y('root')} user."
  prompt_for_new_password!
  
  Spinner.return :message => "Changing root password of #{y('MySQL')} on #{y(e.name)} environment.." do
    e.execute_as_root("mysqladmin -u root --password='#{@existing_password}' password '#{@new_password}'")
    g('Done!')
  end
end

#perform_install!Object

Install MySQL



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 22

def perform_install!
  if e.installed?('mysql')
    error "MySQL is already installed."
    exit
  end
  
  message "Please provide a new password for your MySQL database #{y('root')} user."
  prompt_for_new_password!
  
  e.ensure_aptitude_installed!
  
  command  = "export DEBIAN_FRONTEND=noninteractive; aptitude update;"
  command += "aptitude install -y mysql-client mysql-server libmysqlclient15off libmysqlclient15-dev mysql-common;"
  command += "mysqladmin -u root password '#{@new_password}'"
  
  Spinner.return :message => "Installing #{y('MySQL')} to #{y(e.name)}.." do
    e.execute_as_root(command)
    g('Done!')
  end
end

#perform_remove_user!Object

Removes the user from the database



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 116

def perform_remove_user!
  if not e.installed?('mysql')
    error "MySQL isn't installed."
    exit
  end
  
  ##
  # Confirm Root Password
  message "Please provide your MySQL #{y('root')} password."
  @existing_password = ask('') { |q| q.echo = false }
  confirm_access!
  
  response = e.execute_as_root("mysql -u root --password='#{@existing_password}' -e \"DROP USER '#{c.user}'@'localhost';\"")
  if response =~ /ERROR 1396/
    error "User #{y(c.user)} does not exist."
  elsif response.nil?
    message "User #{y(c.user)} has been removed!"
  else
    puts response
  end
end

#perform_uninstall!Object

Uninstalls the MySQL server



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 66

def perform_uninstall!
  if not e.installed?('mysql')
    error "MySQL isn't installed."
    exit
  end
  
  message "Please provide your MySQL #{y('root')} password."
  @existing_password = ask('') { |q| q.echo = false }
  confirm_access!
  
  Spinner.return :message => "Uninstalling #{y('MySQL')} from #{y(e.name)} environment.." do
    e.execute_as_root("mysqladmin -u root --password='#{@existing_password}' password ''")
    e.execute_as_root("aptitude remove -y mysql-client mysql-server")
    g('Done!')
  end
end

#prompt_for_existing_password!Object

Prompts the user to fill in it’s existing password



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 160

def prompt_for_existing_password!
  while not @mysql_existing_password_confirmed
    message "Please provide your password for your MySQL database root user."
    @existing_password = ask('') { |q| q.echo = false }
    message "Please enter your password again."
    @password_confirmation = ask('')  { |q| q.echo = false }

    if not @existing_password.empty? and @existing_password == @password_confirmation
      @mysql_existing_password_confirmed = true
    else
      if @existing_password.empty? or @password_confirmation.empty?
        error "Please provide a password."
      else
        error "Password and Password confirmation do not match."
      end
    end
  end
end

#prompt_for_new_password!Object

Prompts the user to fill in a new password



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gitpusshuten/modules/mysql/command.rb', line 140

def prompt_for_new_password!
  while not @mysql_new_password_confirmed
    @new_password = ask('') { |q| q.echo = false }
    message "Please enter your password again."
    @password_confirmation = ask('')  { |q| q.echo = false }

    if not @new_password.empty? and @new_password == @password_confirmation
      @mysql_new_password_confirmed = true
    else
      if @new_password.empty? or @password_confirmation.empty?
        error "Please provide a password."
      else
        error "Password and Password confirmation do not match."
      end
    end
  end
end