Class: Adminix::Service
- Inherits:
-
Object
- Object
- Adminix::Service
- Includes:
- Singleton
- Defined in:
- lib/adminix/service.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#completed_commands ⇒ Object
Returns the value of attribute completed_commands.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#process_id ⇒ Object
Returns the value of attribute process_id.
Instance Method Summary collapse
- #config ⇒ Object
- #execute_command(key, process_id, args) ⇒ Object
-
#initialize ⇒ Service
constructor
A new instance of Service.
- #options_to_envs ⇒ Object
- #run_cmd(cmd) ⇒ Object
- #service_commands_payload ⇒ Object
- #service_options_payload ⇒ Object
- #start_watcher ⇒ Object
-
#sync ⇒ Object
Send info about container.
Constructor Details
#initialize ⇒ Service
Returns a new instance of Service.
11 12 13 14 15 16 17 18 |
# File 'lib/adminix/service.rb', line 11 def initialize config.import self.id = config.service_id self.name = config.service_name self.address = config.service_address self.completed_commands = [] end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def address @address end |
#completed_commands ⇒ Object
Returns the value of attribute completed_commands.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def completed_commands @completed_commands end |
#id ⇒ Object
Returns the value of attribute id.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def name @name end |
#process_id ⇒ Object
Returns the value of attribute process_id.
9 10 11 |
# File 'lib/adminix/service.rb', line 9 def process_id @process_id end |
Instance Method Details
#config ⇒ Object
74 75 76 |
# File 'lib/adminix/service.rb', line 74 def config Config.instance end |
#execute_command(key, process_id, args) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/adminix/service.rb', line 105 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 } } puts "-------" puts payload request.body = payload.to_json = {} response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end data = JSON.parse(response.body)['result'] end |
#options_to_envs ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/adminix/service.rb', line 136 def 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 |
#run_cmd(cmd) ⇒ Object
70 71 72 |
# File 'lib/adminix/service.rb', line 70 def run_cmd(cmd) `#{cmd}` end |
#service_commands_payload ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/adminix/service.rb', line 93 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 |
#service_options_payload ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/adminix/service.rb', line 78 def config..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 |
#start_watcher ⇒ Object
154 155 156 157 158 |
# File 'lib/adminix/service.rb', line 154 def start_watcher loop do puts 'ok' end end |
#sync ⇒ Object
Send info about container
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/adminix/service.rb', line 21 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] = payload[:service][:commands] = service_commands_payload end puts payload puts "==========" request.body = payload.to_json = {} response = Net::HTTP.start(uri.hostname, uri.port, ) 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'] || [] puts commands_queue commands_queue.each do |q| if q['status'] != 'processed' execute_command(q['command_key'], q['process_id'], q['args'] || {}) end end end |