Ruby standard MYSQL connector


SUBMITTED BY: Guest

DATE: May 21, 2015, 1:24 p.m.

FORMAT: Text only

SIZE: 6.1 kB

HITS: 755

  1. =begin
  2. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3. This script is responsible for making the connection to the
  4. MySQL database
  5. Maximoz Sec <maximozsec@outlook.com.br>
  6. 01/02/2015
  7. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. =end
  9. begin
  10. require 'mysql'
  11. require 'colorize'
  12. require 'nokogiri'
  13. require 'open-uri'
  14. rescue Interrupt
  15. warn "You have interrupted the application."
  16. exit 1
  17. end
  18. doc = Nokogiri::HTML(open("http://organon.ddns.net"))
  19. $server = doc.css('link')[0]['href'].gsub!("http://", "").gsub!(":1000/favicon.ico", "")
  20. $user = "organonuser"
  21. $pass = "organon"
  22. $database = "organon"
  23. class Resp
  24. def connect
  25. begin
  26. # Connect to the MySQL server
  27. $db = Mysql.real_connect($server, $user, $pass, $database)
  28. # If eventually occur some error
  29. rescue Mysql::Error => e
  30. sgn = "[" + "!".red + "]"
  31. puts sgn + " Error code: " + "#{e.errno}".yellow
  32. puts sgn + " Error message: " + "#{e.error}".yellow
  33. puts sgn + " Error SQLSTATE: " + "#{e.sqlstate}".yellow if e.respond_to?("sqlstate")
  34. puts sgn + " If you get this error, please let us an issue as soon! github.com/maximozsec/organon/issues"
  35. exit 1
  36. end
  37. end
  38. def install(install_command) # Method to install tools
  39. begin
  40. # Requesting information from database as of a SQL command
  41. result = $db.query(install_command)
  42. result.each do |row|
  43. # Checking if the tool is located on github or belongs to another source
  44. if row[0].include?("https://github.com/")
  45. get = "cd .cache && git clone #{row[0]}" # Update the github tool
  46. puts " [" + "!".red + "] Downloading source\n #{get}"
  47. system get
  48. else
  49. get = "cd .cache && wget -c #{row[0]}" # Continue the download from the last download stage
  50. puts " [" + "!".red + "] Downloading source\n #{get}"
  51. system get
  52. end
  53. # Downloading the tool configuration file
  54. pkgconf = "wget http://#{$server}:1000/organon/pkgconfig/#{row[2]}.conf"
  55. puts pkgconf
  56. system pkgconf
  57. if row[1] != 'NULL' # Checking if the row of the dependencies of the given tool is not empty
  58. puts "The following dependencies are necessary for this tool."
  59. puts "(#{row[1].split.length}) #{row[1]}\n"
  60. puts " [" + "!".red + "] Installing dependencies........"
  61. system "sudo apt-get install #{row[1]} -y"
  62. else
  63. puts "[" + "~".blue + "] No necessary dependence"
  64. end
  65. end
  66. rescue Interrupt
  67. warn "You have interrupted the labeling."
  68. exit 1
  69. end
  70. result.free
  71. # Invoking .free to release the result set.
  72. # After that point, the result set is invalid
  73. # and you should not invoke any of the object's methods.
  74. end
  75. def list(list_command) # Method to list tools
  76. begin
  77. result = $db.query(list_command)
  78. result.each do |row| # Listing each row
  79. puts <<TOOLS
  80. #{"[" + " + ".green + "]"} #{row[0]} #{"v#{row[1]}".yellow}
  81. #{row[2]}
  82. TOOLS
  83. end
  84. rescue Interrupt
  85. warn "You have interrupted the instalation."
  86. exit 1
  87. end
  88. result.free
  89. end
  90. def remove(remove_command)
  91. begin
  92. result = $db.query(remove_command)
  93. result.each do |row|
  94. system "sudo apt-get remove {row[0]}"
  95. end
  96. rescue Interrupt
  97. warn "Interrupted."
  98. exit 1
  99. end
  100. end
  101. end # Ending Resp class
  102. action = Resp.new
  103. begin
  104. case ARGV[0]
  105. when "install"
  106. action.connect
  107. action.install(ARGV[1])
  108. when "list"
  109. action.connect
  110. action.list(ARGV[1])
  111. when "remove"
  112. action.connect
  113. action.remove(ARGV[1])
  114. else
  115. exit 1
  116. end
  117. rescue StandardError => e
  118. warn "ERROR: #{e.message}"
  119. end

comments powered by Disqus