require 'securerandom' module Adminix module Entities class Service attr_reader :id, :logs, :load_stamps, :jobs, :device_id, :performed_actions attr_accessor :variables def initialize(opts = {}) @id = opts[:id] @device_id_path = "#{Helpers::Command.home}/.config/adminix/device_id" @variables = [] @jobs = [] @logs = [] @load_stamps = [] @send_logs = false @locked = false @send_system_load = false @performed_actions = { watcher_started: true, watcher_updated: false, process_stopped: false, process_started: false, process_updated: false } define_device_id end def remove_completed_jobs @jobs.each do |job| @jobs -= [job] if job.completed? end end def clean_sent_logs return unless @send_logs @send_logs = false @logs = [] end def clean_load_stamps return unless @send_system_load @send_system_load = false @load_stamps = [] end def add_new_jobs(list) list.each do |data| @jobs << Entities::Job.new( id: data['id'], script: data['script'] ) end end def add_logs(logs) return if locked? @logs += logs end def shift_logs(number) @logs.shift(number) end def add_variable(new_var) var = @variables.find { |v| v.key == new_var.key } if var index = @variables.index(var) @variables[index] = new_var else @variables << new_var end end def add_system_load(sys_load) @load_stamps << sys_load end def shift_load_stamps(number) @load_stamps.shift(number) end def allow_sending_logs @send_logs = true return if @logs.count.zero? Adminix.logger.info("Sending #{@logs.count} new logs") end def allow_sending_system_load @send_system_load = true return if @load_stamps.count.zero? Adminix.logger.info("Sending #{@load_stamps.count} new system load stamps") end def ensure_credentials_exists! return unless id_defined? Helpers::Output.display_error_and_exit('Credentials are not provided') end def define_device_id if Helpers::Files.file_exists?(@device_id_path) read_device_id else generate_device_id end end def read_device_id content = Helpers::Files.read_plain_file(@device_id_path) .split("\n").first.to_s if content.empty? Helpers::Output.display_error_and_exit("Device ID can't be empty") else @device_id = content end end def generate_device_id(id = nil) @device_id = id || SecureRandom.uuid Helpers::Files.write_plain_file(@device_id_path, @device_id) end # TODO: real total memory and disk def sync_payload { device_id: @device_id, watcher_started: @performed_actions[:watcher_started], watcher_updated: @performed_actions[:watcher_updated], process_stopped: @performed_actions[:process_stopped], process_started: @performed_actions[:process_started], process_updated: @performed_actions[:process_updated], completed_jobs: completed_jobs_to_api, logs: logs_to_api, system_load: { stamps: load_stamps_to_api, total: { memory: 1024, disk: 10_240 } } } end def new_jobs @jobs.select(&:in_queue?) end def completed_jobs @jobs.select(&:completed?) end def completed_jobs_to_api completed_jobs.map(&:to_api) end def logs_to_api @send_logs ? @logs.map(&:to_api) : [] end def load_stamps_to_api @send_system_load ? @load_stamps.map(&:to_api) : [] end def reset_performed_actions @performed_actions.keys.map { |key| @performed_actions[key] = false } end def define_performed_action(key, value) @performed_actions[key] = value end def bash_variables(file = nil) output = '' if file content = (variables.map { |v| "#{v.key}=#{v.value}" }).join("\n") Helpers::Files.rm(file) Helpers::Files.write_plain_file(file, content) output = file else variables.each { |v| output << "#{v.to_bash_export}\n" } output << "# Run this command to configure your shell:\n" \ '# eval $(adminix env)' end output end def id_defined? !(id || '').empty? end def new_jobs? new_jobs.any? end def locked? @locked == true end def lock @locked = true end def unlock @locked = false end end end end