#!/usr/bin/env ruby # encoding: utf-8 require 'optparse' require_relative '../lib/adminix' if ENV['DEBUGGING'] == 'true' require 'byebug' end options = { action: 'help', secret_key: ENV['ADMINIX_SECRET_KEY'], service_id: ENV['ADMINIX_SERVICE_ID'], daemonize: false, stop_daemon: false } parsers = {} parsers[:env] = OptionParser.new do |opts| opts.banner = "Display the commands to define ENV variables" opts.separator " Usage: adminix env [options]" opts.on("--file", "generates .env file") { |key| options[:env_file] ||= "/tmp/#{SecureRandom.uuid}.env" } opts.on("--file-path FILE_PATH", ".env file path") { |key| options[:env_file] = key } opts.on("--secret-key SECRET_KEY", "define api SECRET_KEY") { |key| options[:secret_key] = key } opts.on("--service-id SERVICE_ID", "define api SERVICE_ID") { |id| options[:service_id] = id } opts.separator "" end parsers[:define_credentials] = OptionParser.new do |opts| opts.banner = 'Define Adminix credentials' opts.separator " Usage: adminix define_credentials [options]" opts.on("--secret-key SECRET_KEY", "define api SECRET_KEY") { |key| options[:secret_key] = key } opts.on("--service-id SERVICE_ID", "define api SERVICE_ID") { |id| options[:service_id] = id } opts.separator "" end parsers[:download_source] = OptionParser.new do |opts| opts.banner = "Downloads a project source if exists" opts.separator " Usage: adminix download_source" opts.separator "" end parsers[:process_start] = OptionParser.new do |opts| opts.banner = 'Starts a process' opts.separator " Usage: adminix process_start" opts.separator "" end parsers[:process_restart] = OptionParser.new do |opts| opts.banner = 'Restarts a process' opts.separator " Usage: adminix process_restart" opts.separator "" end parsers[:process_stop] = OptionParser.new do |opts| opts.banner = 'Stops a process' opts.separator ' Usage: adminix process_stop' opts.separator '' end parsers[:process_update] = OptionParser.new do |opts| opts.banner = 'Updates a process' opts.separator ' Usage: adminix process_update' opts.separator '' end parsers[:write_systemctl] = OptionParser.new do |opts| opts.banner = 'Creates a systemctl file' opts.separator ' Usage: adminix write_systemctl' opts.separator '' end parsers[:watch] = OptionParser.new do |opts| opts.banner = "Launch Adminix watcher" opts.separator " Usage: adminix watch" opts.separator " Usage: adminix watch [options]" opts.separator " Options:" opts.on("--secret-key SECRET_KEY", "define api SECRET_KEY") { |key| options[:secret_key] = key } opts.on("--service-id SERVICE_ID", "define api SERVICE_ID") { |id| options[:service_id] = id } opts.separator "" end parsers[:version] = OptionParser.new do |opts| opts.banner = "Show the Adminix version" opts.separator " Usage: adminix version" opts.separator "" end options[:action] = ARGV[0] || "help" options[:logger_mode] = options[:action] == 'watch' && ENV['ADMINIX_LOGGER'].nil? ? 'stdout' : nil parsers[options[:action].to_sym].parse!(ARGV) rescue nil Adminix.config = Adminix::Config.new(options) if Adminix.config.credentials_defined? Adminix.config.read_remote_config else Adminix.logger.info 'Adminix is not connected to the cloud! Please go to https://adminix.io to create an account.' end app = Adminix::Services::AppService.new( Adminix::Entities::Service.new( id: Adminix.config.service_id ) ) app.read_vars case options[:action] when 'env' app.service.ensure_credentials_exists! puts app.service.bash_variables(options[:env_file]) when 'download_source' app.service.ensure_credentials_exists! app.download_source when 'process_start' _success, output = app.start_process puts output when 'process_restart' _success, output = app.restart_process puts output when 'process_stop' _success, output = app.stop_process puts output when 'process_update' _success, output = app.update_process puts output when 'upgrade' _success, output = app.upgrade_watcher puts output when 'write_systemctl' Adminix::Helpers::Systemctl.new.write when 'define_credentials' Adminix.config.input(:service_id) if options[:service_id].nil? Adminix.config.input(:secret_key) if options[:secret_key].nil? Adminix.config.export_credentials when 'watch' Adminix::Watcher.run!(app, options) when 'version' puts "adminix version #{Adminix::VERSION}" else puts parsers.map { |v| v[1].to_s } end