Class: EasyMysql

Inherits:
Object
  • Object
show all
Defined in:
lib/easy-mysql.rb

Class Method Summary collapse

Class Method Details

.addLinuxUser(username) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/easy-mysql.rb', line 41

def self.addLinuxUser(username)
	group = username
	password = self.makeRandomPassword

	output = `useradd -p #{password.crypt("JU")} -g #{group} #{username}`

	unless $?.to_i == 0
		raise $?
	end

	return { username: username, password: password }
end

.addMySQLUser(connection, new_username) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/easy-mysql.rb', line 105

def self.addMySQLUser(connection, new_username)
	puts "Creating a new MySQL user..."

	new_password = self.makeRandomPassword

	query = "GRANT SELECT ON *.* TO `#{new_username}`@`127.0.0.1` IDENTIFIED BY '#{new_password}'";

	connection.query(query)

	return { username: new_username, password: new_password }
end

.driverObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/easy-mysql.rb', line 5

def self.driver
	puts " easy-mysql, created by Connor McArthur "

	begin
		 = self.getInputAndMakeMySQLUser
		 = self.getInputAndMakeLinuxUser
	rescue
		return false
	end

	puts "\r\nMySQL".background(:yellow).bright
	puts "Username: {#new_mysql_user_info.username}"
	puts "Password: {#new_mysql_user_info.password}"

	puts "\r\nLinux User".background(:yellow).bright
	puts "Username: {#new_linux_user_info.username}"
	puts "Password: {#new_linux_user_info.password}"

	puts "\r\nSUCCESS!".background(:red).bright
end

.getInputAndMakeLinuxUserObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/easy-mysql.rb', line 26

def self.getInputAndMakeLinuxUser
	puts "\n\rEnter a username for the new Linux user: "

	print "Username: ".bright
	linux_username = gets.chomp

	puts "\n\rPaste in the public key for the new Linux user: "

	print "Public Key Text: ".bright
	linux_pubkey = gets.chomp

	 = self.addLinuxUser(linux_username)
	self.setUpLinuxUserFilesystem(linux_username, linux_pubkey)
end

.getInputAndMakeMySQLUserObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/easy-mysql.rb', line 71

def self.getInputAndMakeMySQLUser
	puts "\n\rEnter your MySQL admin login credentials: "
	
	print "Username: ".bright
	mysql_admin_username = gets.chomp
	print "Password: ".bright
	mysql_admin_password = gets.chomp

	begin
		connection = self.makeMySQLConnection(mysql_admin_username, mysql_admin_password)
	rescue => e
		puts "\r\nERROR! #{e.message}".background(:red).bright
		raise e
	end

	puts "\n\rEnter a username for the new MySQL user: "

	print "Username: ".bright
	mysql_new_username = gets.chomp

	begin
		new_user = self.addMySQLUser(connection, mysql_new_username)
	rescue => e
		puts "\r\nERROR! #{e.message}".background(:red).bright
		raise e
	end

	return new_user
end

.makeMySQLConnection(username, password) ⇒ Object



101
102
103
# File 'lib/easy-mysql.rb', line 101

def self.makeMySQLConnection(username, password)
	Mysql::new('127.0.0.1', username, password)
end

.makeRandomCharacterObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/easy-mysql.rb', line 121

def self.makeRandomCharacter
	type = rand(3)

	if type == 0
		# uppercase letters
		return (65+rand(26)).chr
	elsif type == 1
		# lowercase letters
		return (97+rand(26)).chr
	else
		# numbers
		return rand(9)
	end
end

.makeRandomPassword(len = 24) ⇒ Object



117
118
119
# File 'lib/easy-mysql.rb', line 117

def self.makeRandomPassword(len = 24)
	(0...len).map{self.makeRandomCharacter}.join
end

.setUpLinuxUserFilesystem(username, pubkeytext) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/easy-mysql.rb', line 54

def self.setUpLinuxUserFilesystem(username, pubkeytext)
	begin
		Dir.mkdir("/home/#{username}") unless File.exists?("/home/#{username}")
		Dir.mkdir("/home/#{username}/.ssh") unless File.exists?("/home/#{username}/.ssh")

		File.open("/home/#{username}/.ssh/authorized_keys", 'w') do |file| 
			file.write pubkeytext
		end

		`chown -R #{username}:#{username} /home/#{username}`
		`chmod -R 0600 /home/#{username}/.ssh`
	rescue => e
		puts "\r\nERROR! #{e.message}".background(:red).bright
		raise e
	end
end