class DQS::Core

Public Instance Methods

chmod(path,perm) click to toggle source

Handling chmod permissions

# 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) click to toggle source

Handling chown permissions

# 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) click to toggle source

Handling move files

# 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) click to toggle source

Handling block creation.

# 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) click to toggle source

Handling directory creation…

# 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) click to toggle source

Handling commands TODO: checke if is verbose or not

# 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) click to toggle source

Handling the service actions such as stop/start/restart

# 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) click to toggle source

Handling move files

# 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) click to toggle source

Handling move files

# 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) click to toggle source

Handle configuration files

# 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) click to toggle source

Handling move files

# 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) click to toggle source

Handling move files

# 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) click to toggle source

Save files

# 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) click to toggle source
# 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