saveCert.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. require 'spaceship'
  2. require 'openssl'
  3. require 'mysql2'
  4. require 'json'
  5. require 'pathname'
  6. require 'time'
  7. require Pathname.new(File.dirname(__FILE__)).realpath.to_s + '/Config'
  8. require Pathname.new(File.dirname(__FILE__)).realpath.to_s + '/Login'
  9. require Pathname.new(File.dirname(__FILE__)).realpath.to_s + '/Mysql'
  10. username = ENV["FASTLANE_USER"]
  11. password = ENV["FASTLANE_PASSWORD"]
  12. iss = ARGV[0].to_s
  13. cover = false
  14. if !ARGV[1].empty?
  15. cover = true
  16. end
  17. jsonData = {
  18. "status" => 0,
  19. "msg" => "处理失败"
  20. }
  21. begin
  22. if iss.empty?
  23. raise '请填写iss'
  24. end
  25. userlogin = Login.new(username, password, 1)
  26. userlogin.login()
  27. certificates = Spaceship::Portal.certificate.production.all
  28. if certificates.empty?
  29. csr = File.read(Config::CERT + 'ios.certSigningRequest')
  30. certificateObj = Spaceship::Portal.certificate.production.create!(csr: csr)
  31. cover = 1
  32. else
  33. certificateObj = certificates.first
  34. end
  35. if cover
  36. issPath = Config::CERT + iss
  37. system "mkdir -p #{issPath}"
  38. system "chmod 777 #{issPath}"
  39. pemPath = issPath + "/#{iss}.pem"
  40. File.write(pemPath, certificateObj.download)
  41. p12Path = issPath + "/#{iss}.p12"
  42. keyPath = Config::CERT + 'public/ios.key'
  43. output = system "openssl pkcs12 -export -inkey #{keyPath} -in #{pemPath} -out #{p12Path} -passout pass:123456"
  44. if !output
  45. raise "p12生成失败"
  46. end
  47. total_count = Spaceship::Portal.device.all.length
  48. limit_count = 100 - total_count
  49. if limit_count < 0
  50. limit_count = 0
  51. end
  52. tid = certificateObj.id
  53. client = Mysql.instance.getClient();
  54. client.query("update prefix_super_cert set tid = '#{tid}',total_count = '#{total_count}',limit_count = '#{limit_count}' where username = '#{username}'")
  55. end
  56. jsonData = {
  57. "status" => 1,
  58. "user" => username,
  59. "msg" => "处理成功"
  60. }
  61. rescue Exception => e
  62. if e.message.include?("=>")
  63. response = JSON.parse e.message.gsub('=>', ':')
  64. jsonData = {
  65. "status" => 0,
  66. "msg" => "处理失败",
  67. "user" => username,
  68. "response" => response
  69. }
  70. else
  71. jsonData = {
  72. "status" => 0,
  73. "user" => username,
  74. "msg" => e.message
  75. }
  76. end
  77. ensure
  78. Mysql.instance.close()
  79. puts JSON[jsonData]
  80. end