require 'singleton' require 'json' require 'net/http' module Adminix class Service include Singleton attr_accessor :id, :process_id, :name, :address, :completed_commands def initialize config.import self.id = config.service_id self.name = config.service_name self.address = config.service_address self.completed_commands = [] end # Send info about container def sync uri = URI.parse("#{config.host}/v1/services/sync") request = Net::HTTP::Post.new(uri) request.content_type = "application/json" request["Authorization"] = "Bearer #{config.secret_key}" payload = { service: { id: id, process_id: process_id, completed_commands: completed_commands } } if process_id.nil? payload[:service][:name] = name payload[:service][:address] = address payload[:service][:options] = service_options_payload payload[:service][:commands] = service_commands_payload end request.body = payload.to_json req_options = {} response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end data = JSON.parse(response.body)['result'] self.completed_commands = [] self.process_id = data['process_id'] if id.nil? self.id = data['id'] config.service_id = id config.export end commands_queue = data['commands_queue'] || [] commands_queue.each do |q| if q['status'] != 'processed' execute_command(q['command_key'], q['process_id'], q['args'] || {}) end end end def run_cmd(cmd) `#{cmd}` end def config Config.instance end def service_options_payload config.options.map do |o| { key: o['key'], name: o['name'], description: o['description'], value_type: o['type'], options: o['options'], validations: o['validations'], default_value: o['default_value'], value: o['default_value'] } end end def service_commands_payload config.commands.map do |o| { key: o['key'], name: o['name'], description: o['description'], #command: o['command'], arguments: o['arguments'] } end end def execute_command(key, process_id, args) command = config.commands.find { |c| c['key'] == key } script = command['command'].dup # TODO frontend fix attribute args args['args'].each { |hs| script.gsub!("%{#{hs[0]}}", hs[1]) } puts System.instance.eval(script) #self.completed_commands << process_id uri = URI.parse("#{config.host}/v1/services/#{id}") request = Net::HTTP::Put.new(uri) request.content_type = "application/json" request["Authorization"] = "Bearer #{config.secret_key}" payload = { service: { completed_command_process_id: process_id } } request.body = payload.to_json req_options = {} response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(request) end data = JSON.parse(response.body)['result'] end def options_to_envs uri = URI.parse("#{config.host}/v1/services/#{id}/options") request = Net::HTTP::Get.new(uri) request["Authorization"] = "Bearer #{config.secret_key}" response = Net::HTTP.start(uri.hostname, uri.port, {}) do |http| http.request(request) end data = JSON.parse(response.body) data['result'].each do |o| puts "export #{o['key'].upcase}=#{o['value']}" end puts "# Run this command to configure your shell:\n" + "# eval $(adminix env)" end def start_watcher loop do puts 'ok' end end end end