Class: DQS::Core

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

Constant Summary collapse

@@dpkg =

Instance variables…

'/usr/bin/dpkg'
@@aptitude =
'/usr/bin/aptitude'
@@mknod =
'/bin/mknod'
@@grep =
'/bin/grep --color -i -e'
@@systemctl =
'/bin/systemctl'
@@awk =
'/usr/bin/awk'

Instance Method Summary collapse

Instance Method Details

#chmod(path, perm) ⇒ Object

Handling chmod permissions



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/dqscore.rb', line 190

def chmod(path,perm)
	str_perm = perm.to_s(8)
	begin
		if Dir.exists?(path) || File.exists?(path)
			if FileUtils.chmod_R(perm,"#{path}")
				puts "#{Tty.white}Path #{Tty.blue} ==> #{Tty.white} #{path} #{Tty.blue} ==> #{Tty.white} permission changed to: #{str_perm} #{Tty.blue} ==> #{Tty.green} permission Changed #{Tty.reset}"
			end
		end
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
end

#chown(path, owner = nil, group = nil) ⇒ Object

Handling chown permissions



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/dqscore.rb', line 205

def chown(path,owner=nil,group=nil)
	begin
		if Dir.exists?(path) || File.exists?(path)
			if FileUtils.chown_R(owner, group, "#{path}")
				if group.nil?
					puts "#{Tty.white}Path #{Tty.blue} ==> #{Tty.white} #{path} #{Tty.blue} ==> #{Tty.white} changed to: owner: #{owner} #{Tty.blue} ==> #{Tty.green} Changed #{Tty.reset}"
				else
					puts "#{Tty.white}Path #{Tty.blue} ==> #{Tty.white} #{path} #{Tty.blue} ==> #{Tty.white} changed to: owner: #{owner}, group: #{group} #{Tty.blue} ==> #{Tty.green} Changed #{Tty.reset}"
				end
			end
		end
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
end

#cp(src, dst) ⇒ Object

Handling move files



134
135
136
137
138
139
140
141
142
# File 'lib/dqscore.rb', line 134

def cp(src,dst)
	begin
		FileUtils.cp_r("#{src}","#{dst}", :preserve => true)
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
	puts "#{Tty.white}Path #{Tty.blue} ==> #{Tty.white} #{src} #{Tty.blue} ==> #{Tty.white} copied to: #{dst} #{Tty.blue} ==> #{Tty.green} Copied #{Tty.reset}"
end

#create_block(device, opt) ⇒ Object

Handling block creation.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dqscore.rb', line 93

def create_block(device,opt)
	begin
		if File.exists?(device)
			puts "#{Tty.white}Device #{Tty.blue} ==> #{Tty.white} #{device} #{Tty.blue} ==> #{Tty.green} Already Exists #{Tty.reset}"
		elsif system("#{@@mknod} #{device} #{opt}")
			puts "#{Tty.white}Created device #{Tty.blue} ==> #{Tty.green} #{device} #{Tty.reset}"
		end
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end

end

#create_dir(dir) ⇒ Object

Handling directory creation…



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dqscore.rb', line 108

def create_dir(dir)
	begin
		if Dir.exists?(dir)
			puts "#{Tty.white}Directory #{Tty.blue} ==> #{Tty.white} #{dir} #{Tty.blue} ==> #{Tty.green} Already Exists #{Tty.reset}"
		elsif FileUtils.mkdir_p(dir)
			puts "#{Tty.white}Created directory #{Tty.blue} ==> #{Tty.green} #{dir} #{Tty.reset}"
		end
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
end

#exec_cmd(command) ⇒ Object

Handling commands TODO: checke if is verbose or not



146
147
148
149
150
151
152
153
154
# File 'lib/dqscore.rb', line 146

def exec_cmd(command)
	begin
		system("#{command} >> /dev/null")
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
	puts "#{Tty.white}Command #{Tty.blue} ==> #{Tty.white} #{command} #{Tty.blue} ==> #{Tty.green} Executed #{Tty.reset}"
end

#handle_service(service = nil, action = nil) ⇒ Object

Handling the service actions such as stop/start/restart



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/dqscore.rb', line 236

def handle_service(service=nil,action=nil)
	service_name = service.split('.').first
	begin
		if action.nil?
			puts "#{Tty.white}Executing service #{Tty.blue} ==> #{Tty.green} #{service_name.upcase} #{Tty.reset}"
			system("#{@@systemctl} #{service}")
		else
			puts "#{Tty.white}Executing service #{Tty.blue} ==> #{Tty.white} #{service_name} #{Tty.blue} ==> #{Tty.green} #{action.upcase} #{Tty.reset}"
			system("#{@@systemctl} #{action} #{service}")
		end
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
	if action.nil?
		puts "#{Tty.white}Service #{Tty.blue} ==> #{Tty.green} #{service_name.upcase} #{Tty.reset}"
	else
		puts "#{Tty.white}Service #{Tty.blue} ==> #{Tty.white} #{service_name} #{Tty.blue} ==> #{Tty.green} #{action.upcase} #{Tty.reset}"
	end
end

#ln(src, dst) ⇒ Object

Handling move files



179
180
181
182
183
184
185
186
187
# File 'lib/dqscore.rb', line 179

def ln(src,dst)
	begin
		FileUtils.ln("#{src}","#{dst}")
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
	puts "#{Tty.white}Path #{Tty.blue} ==> #{Tty.white} #{src} #{Tty.blue} ==> #{Tty.white} linked to: #{dst} #{Tty.blue} ==> #{Tty.green} Linked #{Tty.reset}"
end

#ln_s(src, dst) ⇒ Object

Handling move files



168
169
170
171
172
173
174
175
176
# File 'lib/dqscore.rb', line 168

def ln_s(src,dst)
	begin
		FileUtils.ln_s("#{src}","#{dst}")
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
	puts "#{Tty.white}Path #{Tty.blue} ==> #{Tty.white} #{src} #{Tty.blue} ==> #{Tty.white} linked to: #{dst} #{Tty.blue} ==> #{Tty.green} Linked #{Tty.reset}"
end

#load_conf(path = nil) ⇒ Object

Handle configuration files



258
259
260
261
262
263
264
265
# File 'lib/dqscore.rb', line 258

def load_conf(path=nil)
	parsed = begin
  YAML.load(File.open("#{path}"))
	rescue Exception => e
	  puts "Could not parse YAML: #{e.message}"
	end
	return to_ostruct(parsed)
end

#mv(src, dst) ⇒ Object

Handling move files



122
123
124
125
126
127
128
129
130
131
# File 'lib/dqscore.rb', line 122

def mv(src,dst)
	begin
		FileUtils.cp_r("#{src}","#{dst}", :preserve => true)
		FileUtils.rm_rf("#{src}")
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
	puts "#{Tty.white}Path #{Tty.blue} ==> #{Tty.white} #{src} #{Tty.blue} ==> #{Tty.white} moved to: #{dst} #{Tty.blue} ==> #{Tty.green} Moved #{Tty.reset}"
end

#rm(path) ⇒ Object

Handling move files



157
158
159
160
161
162
163
164
165
# File 'lib/dqscore.rb', line 157

def rm(path)
	begin
		FileUtils.rm_rf("#{path}")
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
	puts "#{Tty.white}Path #{Tty.blue} ==> #{Tty.white} #{path} #{Tty.blue} ==> #{Tty.green} Removed #{Tty.reset}"
end

#save_file(content, path) ⇒ Object

Save files



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/dqscore.rb', line 223

def save_file(content,path)
	begin
		file = File.new("#{path}",'w')
      		file.write content
			file.close
	rescue Exception => e
		puts "#{Tty.red} Error: #{Tty.blue}==>#{Tty.red} #{e.message}!#{Tty.reset}"
		exit!
	end
	puts "#{Tty.white}File #{Tty.blue} ==> #{Tty.white} #{path} #{Tty.blue} ==> #{Tty.green} Saved #{Tty.reset}"
end

#to_ostruct(object) ⇒ Object



267
268
269
270
271
272
273
274
275
276
# File 'lib/dqscore.rb', line 267

def to_ostruct(object)
  case object
  when Hash
    OpenStruct.new(Hash[object.map {|k, v| [k, to_ostruct(v)] }])
  when Array
    object.map {|x| to_ostruct(x) }
  else
    object
  end
end