354 lines
13 KiB
Ruby
Executable File
354 lines
13 KiB
Ruby
Executable File
class License
|
|
include HTTParty
|
|
|
|
base_uri "provision.zsai.ws/api"
|
|
|
|
attr_accessor :name, :address_1, :address_2, :township, :city, :country, :email, :phone, :fax, :logo, :subdomain,
|
|
:plan_activation_date, :plan_next_renewal_date, :plan_max_products,:plan_max_customers, :plan_active_connections,
|
|
:dbhost, :dbschema, :dbusername, :dbpassword, :exchange_unqiue_id, :localqueue_host,:server_mode,:localhost_address,
|
|
:localqueue_user, :localqueue_password, :remotequeue_host, :remotequeue_user, :remotequeue_password, :api_token, :app_token
|
|
|
|
@license = nil
|
|
@secret = nil
|
|
|
|
def initialize(server = "", lookup = "")
|
|
#this code is hard-code to reflect server mode - Very important.
|
|
self.server_mode = ENV["SERVER_MODE"]
|
|
|
|
if (server != "")
|
|
self.class.base_uri server
|
|
end
|
|
|
|
# @secret = ENV["aes_key"]
|
|
# @params = { query: { device: "SX", token: SECRETS_CONFIG['provision_key'] } }
|
|
end
|
|
|
|
def detail_with_local_cache(lookup, key, iv)
|
|
##Check from local redis - if available load local otherwise get from remote
|
|
cache_key = "#{lookup}:license:#{key}:hostname"
|
|
|
|
# No Needs for current
|
|
# @secret = key
|
|
|
|
cache_license = nil
|
|
|
|
##Get redis connection from connection pool
|
|
redis = Redis.new
|
|
cache_license = redis.get(cache_key)
|
|
# Redis.current do |conn|
|
|
# cache_license = conn.get(cache_key)
|
|
# end
|
|
|
|
Rails.logger.info "Cache key - " + cache_key.to_s
|
|
if cache_license.nil?
|
|
##change the d/e key
|
|
# @options = { query: {device: "SXlite", lookup: lookup, skey: @secret, token: SECRETS_CONFIG['provision_key']} }
|
|
@params = { query: { lookup_type: self.server_mode, lookup: lookup, iv_key: iv} }
|
|
response = self.class.get("/subdomain", @params)
|
|
@license = response.parsed_response
|
|
|
|
if (@license["status"] == true)
|
|
assign()
|
|
|
|
Rails.logger.info "License - " + response.parsed_response.to_s
|
|
|
|
redis = Redis.new
|
|
redis.set(cache_key, Marshal.dump(@license))
|
|
# redis.sadd("License:cache:keys", cache_key)
|
|
# Redis.current do |conn|
|
|
# ##Remote - store the remote response in local redis cache
|
|
# conn.set(cache_key, Marshal.dump(@license))
|
|
# ##ADD to List to remove later
|
|
# conn.sadd("License:cache:keys", cache_key)
|
|
# end
|
|
|
|
return true
|
|
end
|
|
else
|
|
@license = Marshal.load(cache_license)
|
|
assign()
|
|
Rails.logger.info 'API License'
|
|
return true
|
|
end
|
|
end
|
|
|
|
|
|
def detail_with_local_file()
|
|
has_license = true #verify_license()
|
|
|
|
if has_license
|
|
# puts "VERIFIED"
|
|
end
|
|
end
|
|
|
|
# License Activation
|
|
def license_activate (key, iv, license_key, db_host, db_schema, db_user, db_password)
|
|
@params = { query: { lookup_type: self.server_mode, iv_key: iv, license_key: license_key } }
|
|
response = self.class.get("/activate", @params)
|
|
@activate = response.parsed_response
|
|
|
|
Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s
|
|
if (@activate["status"])
|
|
response = create_license_file(@activate)
|
|
if(response[:status])
|
|
sym_path = "/home/yan/symmetric/"
|
|
response = create_symmetric_config(sym_path, db_host, db_schema, db_user, db_password)
|
|
if(response[:status])
|
|
response = run_symmetric(sym_path)
|
|
end
|
|
end
|
|
else
|
|
response = { "status": false, "message": "Activation Failed! Please contact code2lab call center!"}
|
|
end
|
|
return response
|
|
end
|
|
|
|
def verify_license
|
|
api_token = read_license("api_token")
|
|
@params = { query: {lookup_type: "application", token: api_token} }
|
|
response = self.class.get("/verify", @params)
|
|
@varified = response.parsed_response
|
|
|
|
Rails.logger.debug "License Remote Response - " + response.parsed_response.to_s
|
|
if (@varified["status"])
|
|
if (!check_expired(@varified["plan_next_renewal_date"]))
|
|
return true
|
|
end
|
|
else
|
|
delete_license_file
|
|
end
|
|
return false
|
|
end
|
|
|
|
# Check License expired date from PROVISION SERVER
|
|
def check_expired(renewal_date)
|
|
if (renewal_date < Date.today)
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
# Check License File exists
|
|
def self.check_license_file
|
|
return File.exist?("config/license.yml")
|
|
end
|
|
|
|
# read line by key for license file
|
|
def read_license(key)
|
|
decrypted_line = ""
|
|
if File.exist?("config/license.yml")
|
|
File.open("config/license.yml").each do |line|
|
|
if line.include? (key)
|
|
decrypted_line_array = line.split(": ")
|
|
decrypted_line = AESCrypt.decrypt_data(decode_str(decrypted_line_array[1]), decode_str(ENV['AES_KEY']), decode_str(ENV['AES_IV']), ENV['CIPHER_TYPE'])
|
|
end
|
|
end
|
|
end
|
|
return decrypted_line
|
|
end
|
|
|
|
private
|
|
|
|
def decode_str(str)
|
|
return Base64.decode64(str)
|
|
end
|
|
|
|
# License File Creation
|
|
def create_license_file(response_data)
|
|
if File.exist?("config/license.yml")
|
|
delete_license_file
|
|
end
|
|
|
|
begin
|
|
# Licese File Creation
|
|
File.open("config/license.yml", "w") do |f|
|
|
f.puts("iv_key: #{response_data['iv_key']}")
|
|
f.puts("shop_name: #{response_data['shop_name']}")
|
|
f.puts("email: #{response_data['email']}")
|
|
f.puts("telephone: #{response_data['telephone']}")
|
|
f.puts("fax: #{response_data['fax']}")
|
|
f.puts("address: #{response_data['address']}")
|
|
f.puts("dbhost: #{response_data['dbhost']}")
|
|
f.puts("dbschema: #{response_data['dbschema']}")
|
|
f.puts("dbusername: #{response_data['dbusername']}")
|
|
f.puts("dbpassword: #{response_data['dbpassword']}")
|
|
f.puts("api_token: #{response_data['api_token']}")
|
|
f.puts("app_token: #{response_data['app_token']}")
|
|
end
|
|
rescue IOError
|
|
response = { "status": false, "message": "Activation is success but something is wrong. \n Please contact code2lab call center!"}
|
|
end
|
|
response = { "status": true, "message": "Success Activation. License also created."}
|
|
end
|
|
|
|
# Symmetric Configuration
|
|
def create_symmetric_config(sym_location, db_host, db_schema, db_user, db_password)
|
|
if File.directory? (sym_location)
|
|
begin
|
|
# sx properties create
|
|
f = File.open(sym_location + "engines/sx.properties", "w")
|
|
f.write("engine.name=sx\n")
|
|
f.write("db.driver=com.mysql.jdbc.Driver\n")
|
|
f.write("db.url=jdbc:mysql://#{db_host}/#{db_schema}?tinyInt1isBit=false\n")
|
|
f.write("db.user=#{db_user}\n")
|
|
f.write("db.password=#{db_password}\n")
|
|
f.write("registration.url=\n")
|
|
f.write("sync.url=http://#{db_host}:31415/sync/sx\n")
|
|
f.write("group.id=sx\n")
|
|
f.write("external.id=000\n")
|
|
f.write("job.purge.period.time.ms=7200000\n")
|
|
f.write("job.routing.period.time.ms=5000\n")
|
|
f.write("job.push.period.time.ms=10000\n")
|
|
f.write("job.pull.period.time.ms=10000\n")
|
|
f.write("initial.load.create.first=true\n")
|
|
f.write("initial.load.use.extract.job.enabled=true\n")
|
|
f.close
|
|
byebug
|
|
# read from license file
|
|
shop_name = read_license("shop_name")
|
|
dbhost = read_license("dbhost")
|
|
dbschema = read_license("dbschema")
|
|
dbusername = read_license("dbusername")
|
|
dbpassword = read_license("dbpassword")
|
|
|
|
# shop properties create
|
|
f = File.open(sym_location + "/#{shop_name}.properties", "w")
|
|
f.write("engine.name=#{shop_name}\n")
|
|
f.write("db.driver=com.mysql.jdbc.Driver\n")
|
|
f.write("db.url=jdbc:mysql://#{dbhost}/#{dbschema}?tinyInt1isBit=false\n")
|
|
f.write("db.user=#{dbusername}\n")
|
|
f.write("db.password=#{dbpassword}\n")
|
|
f.write("registration.url=http://#{db_host}:31415/sync/sx\n")
|
|
f.write("group.id=store\n")
|
|
f.write("external.id=001\n")
|
|
f.write("job.routing.period.time.ms=5000\n")
|
|
f.write("job.push.period.time.ms=10000\n")
|
|
f.write("job.pull.period.time.ms=10000\n")
|
|
# f.write("initial.load.create.first=true\n")
|
|
# f.write("initial.load.use.extract.job.enabled=true\n")
|
|
f.close
|
|
rescue IOError
|
|
response = { "status": false, "message": "Activation is success but something is wrong. \n Please contact code2lab call center!"}
|
|
end
|
|
response = { "status": true, "message": "Success Activation. License also created."}
|
|
end
|
|
end
|
|
|
|
# Run Symmetric
|
|
def run_symmetric(sym_path)
|
|
# check_sym_proc_str = `#{sym_path + "bin/sym_service status"}`
|
|
# check_sym_proc_str = check_sym_proc_str.split("\n")
|
|
# sym_install_status = check_sym_proc_str[0].split(": ")
|
|
|
|
check_sym_proc_str = `#{"service SymmetricDS status"}`
|
|
|
|
# Check Sym Installed
|
|
# if sym_install_status[1] == "false"
|
|
# response = { "status": false, "message": "Activation is success but Symmetric service not Installed. \n Please contact code2lab call center!"}
|
|
# end
|
|
byebug
|
|
# Run Sym Service
|
|
sym_run_status = check_sym_running(check_sym_proc_str, sym_path)
|
|
if sym_run_status
|
|
# Create Sym Table
|
|
check_sym_table = system(sym_path + "bin/symadmin --engine sx create-sym-tables")
|
|
if check_sym_table
|
|
sym_sql = Rails.root + "db/sym_master.sql"
|
|
if File.exist? (sym_sql)
|
|
# Import Sym Sql to db and start sym
|
|
run_sym_sql = system(sym_path + "bin/dbimport --engine sx " + sym_sql)
|
|
run_sym = system(sym_path + "bin/sym")
|
|
if run_sym
|
|
response = { "status": true, "message": "Activation is success and Configuration done..."}
|
|
end
|
|
else
|
|
response = { "status": false, "message": "Activation is success but Symmetric Sql not Found. \n Please contact code2lab call center!"}
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
# Check Symmetric Running
|
|
def check_sym_running(status, sym_path)
|
|
# Run Sym Service
|
|
# if status.include? "Server is already running"
|
|
# return true
|
|
# elsif status.include? "false"
|
|
# sym_start_str = `#{sym_path + "bin/sym_service start"}`
|
|
# if sym_start_str.include? "Started"
|
|
# return true
|
|
# else
|
|
# check_sym_running(sym_start_status[0])
|
|
# end
|
|
# else
|
|
# return true
|
|
# end
|
|
|
|
if status.include? "Active: active (running)" #"Server is already running"
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
# Delete License File
|
|
def delete_license_file
|
|
File.delete("config/license.yml") if File.exist?("config/license.yml")
|
|
end
|
|
|
|
# Assign db info for Cloud
|
|
def assign
|
|
# self.name = @license["name"]
|
|
# self.address_1 = @license["address_1"]
|
|
# self.address_2 = @license["address_2"]
|
|
# self.township = @license["township"]
|
|
# self.city = @license["city"]
|
|
# self.country = @license["country"]
|
|
# self.email = @license["email"]
|
|
# self.phone = @license["phone"]
|
|
# self.fax = @license["fax"]
|
|
# self.logo = @license["logo"]
|
|
# self.localhost_address = @license["localhost_address"]
|
|
# self.subdomain = @license["subdomain"]
|
|
# self.plan_activation_date = Date.parse(@license["plan_activation_date"])
|
|
# self.plan_next_renewal_date = Date.parse(@license["plan_next_renewal_date"])
|
|
|
|
## self.plan_activation_date = Date.strptime(@license["plan_activation_date"], "%Y-%m-%d")
|
|
## self.plan_next_renewal_date = Date.strptime(@license["plan_next_renewal_date"], "%Y-%m-%d")
|
|
|
|
# self.plan_max_products = @license["plan_max_products"].to_i
|
|
# self.plan_max_customers = @license["plan_max_customers"].to_i
|
|
# self.plan_active_connections = @license["plan_active_connections"].to_i
|
|
# salt = @license["secret_key"]
|
|
|
|
key = Base64.decode64(ENV['AES_KEY'])
|
|
iv = Base64.decode64(ENV['AES_IV'])
|
|
|
|
if (@license["dbhost"] || @license["dbschema"] || @license["dbusername"] || @license["dbpassword"] )
|
|
host = Base64.decode64(@license["dbhost"])
|
|
dbschema = Base64.decode64(@license["dbschema"])
|
|
dbusername = Base64.decode64(@license["dbusername"])
|
|
dbpassword = Base64.decode64(@license["dbpassword"])
|
|
|
|
self.dbhost = AESCrypt.decrypt_data(host, key, iv, ENV['CIPHER_TYPE'])
|
|
self.dbschema = AESCrypt.decrypt_data(dbschema, key, iv, ENV['CIPHER_TYPE'])
|
|
self.dbusername = AESCrypt.decrypt_data(dbusername, key, iv, ENV['CIPHER_TYPE'])
|
|
self.dbpassword = AESCrypt.decrypt_data(dbpassword, key, iv, ENV['CIPHER_TYPE'])
|
|
end
|
|
|
|
# self.exchange_unqiue_id = @license["exchange_unqiue_id"]
|
|
|
|
# self.localqueue_host= @license["localqueue_host"]
|
|
# self.localqueue_user= @license["localqueue_user"]
|
|
# self.localqueue_password= @license["localqueue_password"]
|
|
|
|
# self.remotequeue_host = @license["remotequeue_host"]
|
|
# self.remotequeue_user = @license["remotequeue_user"]
|
|
# self.remotequeue_password = @license["remotequeue_password"]
|
|
|
|
# self.api_token = @license["api_token"]
|
|
# self.app_token = @license["app_token"]
|
|
end
|
|
end
|