module Adminix module Services class AppService attr_reader :service def initialize(service) @service = service end def sync return if !Adminix.config.credentials_defined? || @service.locked? service.lock payload = service.sync_payload success, result = Helpers::NetHTTP.post( "services/#{service.id}/sync_device", payload ) unless success service.unlock return end service.reset_performed_actions service.remove_completed_jobs service.clean_sent_logs service.clean_load_stamps service.add_new_jobs(result['jobs']) upgrade_watcher if result['upgrade_watcher'] == true start_process if result['start_process'] == true stop_process if result['stop_process'] == true restart_process if result['restart_process'] == true update_process if result['update_process'] == true service.unlock # rescue => e # service.unclock end def run_jobs @service.new_jobs[0..1].each(&:perform) end def allow_sending_logs @service.allow_sending_logs end def allow_sending_system_load @service.allow_sending_system_load end def upgrade_watcher Adminix.logger.info 'Upgrading watcher' out = Helpers::Command.run_system_command( Adminix.config.scripts[:upgrade_watcher] ) [true, out] end def update_process Adminix.logger.info 'Updating process' out = Helpers::Command.run_system_command( Adminix.config.scripts[:update_process] ) service.define_performed_action(:process_updated, true) [true, out] end def start_process Adminix.logger.info 'Starting process' out = Helpers::Command.run_system_command( Adminix.config.scripts[:start_process] ) service.define_performed_action(:process_stopped, false) service.define_performed_action(:process_started, true) [true, out] end def restart_process Adminix.logger.info 'Restarting process' stop_process _, out = start_process [true, out] end def stop_process Adminix.logger.info 'Stopping process' out = Helpers::Command.run_system_command( Adminix.config.scripts[:stop_process] ) service.define_performed_action(:process_started, false) service.define_performed_action(:process_stopped, true) [true, out] end def download_source read_vars list = @service.variables repo_var = list.find { |v| v.key == 'git_repo' } branch_var = list.find { |v| v.key == 'git_branch' } if repo_var && repo_var.value && branch_var Helpers::Command.git_clone(repo_var.value, branch_var.value) else puts 'Please define your GIT repository and branch' end end def read_vars read_local_vars read_remote_vars write_local_vars end def read_local_vars vars_file_path = Adminix.config.config_file('vars') return unless File.exist?(vars_file_path) vars = Helpers::Files.read_yaml_file(vars_file_path) vars.each do |k, v| @service.add_variable Entities::Variable.new(key: k, value: v) end end def read_remote_vars Entities::Variable.all(@service).each { |v| @service.add_variable(v) } end def write_local_vars vars_file_path = Adminix.config.config_file('vars') data = {} @service.variables.each { |v| data[v.key] = v.value } Helpers::Files.write_yaml_file(vars_file_path, data) end end end end