diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb index 07951bf..b21329f 100644 --- a/app/controllers/hosts_controller.rb +++ b/app/controllers/hosts_controller.rb @@ -1,198 +1,198 @@ class HostsController < ApplicationController before_action :require_logged_in, except: :fd_config before_action :fetch_host, only: [:show, :edit, :update, :destroy, :submit_config, :revoke, :disable] before_action :fetch_hosts_of_user, only: [:new, :create] # GET /hosts/new def new @host = Host.new @host.port = 9102 @host.email_recipients = [current_user.email] end # POST /hosts def create @host = Host.new(fetch_params) set_host_type @host.verified = !@host.institutional? if user_can_add_this_host? && @host.save flash[:success] = 'Host created successfully' current_user.hosts << @host UserMailer.notify_admin(current_user, @host.fqdn).deliver redirect_to host_path @host else flash[:error] = 'Host was not created' render :new end end # GET /hosts/1 def show @schedules = @host.job_templates.map(&:schedule) @filesets = @host.job_templates.map(&:fileset) end # GET /hosts/1/edit def edit if current_user.needs_host_list? @hosts_of_user = current_user.hosts.pluck(:fqdn) end end # PATCH /hosts/1 def update updates = fetch_params.slice(:port, :password, :email_recipients) if updates.present? && @host.update_attributes(updates) @host.recalculate if @host.bacula_ready? - flash[:success] = 'Host updated successfully. You must update your file deamon accordingly.' + flash[:success] = 'Host updated successfully. You must update your file daemon accordingly.' redirect_to host_path @host else render :edit end end # DELETE /hosts/1 def destroy if (@host.client.nil? || @host.remove_from_bacula(true)) && @host.destroy flash[:success] = 'Host destroyed successfully' else flash[:error] = 'Host not destroyed' end redirect_to root_path end # POST /hosts/1/disable def disable if @host.disable_jobs_and_update flash[:success] = 'Client disabled' else flash[:error] = 'Something went wrong, try again later' end redirect_to host_path(@host) end # POST /hosts/1/submit_config def submit_config if @host.dispatch_to_bacula flash[:success] = 'Host configuration sent to Bacula successfully' else flash[:error] = 'Something went wrong, try again later' end redirect_to host_path(@host) end # DELETE /hosts/1/revoke def revoke if @host.remove_from_bacula flash[:success] = 'Host configuration removed from Bacula successfully' else flash[:error] = 'Something went wrong, try again later' end redirect_to root_path end # GET /hosts/fetch_vima_hosts def fetch_vima_hosts if params[:code].blank? return redirect_to client.auth_code.authorize_url(:redirect_uri => redirect_uri, scope: 'read') end access_token = client.auth_code.get_token( params['code'], { :redirect_uri => redirect_uri }, { :mode => :query, :param_name => "access_token", :header_format => "" } ) vms = access_token.get( 'https://vima.grnet.gr/instances/list?tag=vima:service:archiving', { mode: :query, param_name: 'access_token' } ).parsed.deep_symbolize_keys[:response][:instances] session[:vms] = vms.first(50) current_user.temp_hosts = vms current_user.hosts_updated_at = Time.now current_user.save Host.where(fqdn: vms).each do |host| host.users << current_user unless host.users.include?(current_user) end redirect_to new_host_path end # GET /hosts/:id/fd_config?token=A_TOKEN def fd_config user = User.find_by(token: params[:token]) if params[:token] return redirect_to clients_path if user.nil? @host = user.hosts.find_by(id: params[:id]) return redirect_to clients_path unless @host render text: [ - @host.bacula_fd_filedeamon_config, + @host.bacula_fd_filedaemon_config, @host.bacula_fd_director_config(false), @host.bacula_fd_messages_config ].join("\n\n") end private def client OAuth2::Client.new( Rails.application.secrets.oauth2_vima_client_id, Rails.application.secrets.oauth2_vima_secret, site: 'https://vima.grnet.gr', token_url: "/o/token", authorize_url: "/o/authorize", :ssl => {:ca_path => "/etc/ssl/certs"} ) end def redirect_uri uri = URI.parse(request.url) uri.scheme = 'https' unless Rails.env.development? uri.path = '/hosts/fetch_vima_hosts' uri.query = nil uri.to_s end def fetch_hosts_of_user return if not current_user.needs_host_list? @hosts_of_user = session[:vms] - current_user.hosts.pluck(:fqdn) end def fetch_host @host = current_user.hosts.includes(job_templates: [:fileset, :schedule]).find(params[:id]) end def fetch_params params.require(:host).permit(:fqdn, :port, :password, email_recipients: []) end def user_can_add_this_host? !current_user.needs_host_list? || @hosts_of_user.include?(@host.fqdn) end def set_host_type @host.origin = if current_user.vima? :vima elsif current_user.okeanos? :okeanos else :institutional end end end diff --git a/app/views/hosts/_fd_config.html.erb b/app/views/hosts/_fd_config.html.erb index add31c9..60cc110 100644 --- a/app/views/hosts/_fd_config.html.erb +++ b/app/views/hosts/_fd_config.html.erb @@ -1,23 +1,23 @@
-

Client FileDeamon Config

+

Client FileDaemon Config

-<%= @host.bacula_fd_filedeamon_config %>
+<%= @host.bacula_fd_filedaemon_config %>
   

Client Director Config

 <%= @host.bacula_fd_director_config %>
   

Client Messages Config

 <%= @host.bacula_fd_messages_config %>
   
<%= link_to 'Download Config', fd_config_host_path(@host, token: current_user.token), class: "btn btn-success", role: "button", download: 'fd_config' %>
diff --git a/lib/configuration/host.rb b/lib/configuration/host.rb index 5050ee3..e6ecd3b 100644 --- a/lib/configuration/host.rb +++ b/lib/configuration/host.rb @@ -1,142 +1,142 @@ module Configuration # Helper module to add configuration getters for Host module Host # Constructs the final Bacula configuration for the host by appending configs for # # * Client # * Jobs # * Schedules # * Filesets # # by calling their `to_bacula_config_array` methods. # # @return [Array] containing each element's configuration line by line def baculize_config templates = job_templates.includes(:fileset, :schedule) result = [self] + templates.map {|x| [x, x.fileset, x.schedule] }.flatten.compact.uniq result.map(&:to_bacula_config_array) end # Constructs the final Bacula configuration for the host by appending configs for # # * Client # * Jobs # * Schedules # * Filesets # # by calling their `to_bacula_config_array` methods. # # It hides the password. # # @return [Array] containing each element's configuration line by line def baculize_config_no_pass baculize_config.join("\n").gsub(/Password = ".*"$/, 'Password = "*************"') end # Constructs an array where each element is a line for the Client's bacula config # # @return [Array] def to_bacula_config_array [ "Client {", " Name = #{name}", " Address = #{fqdn}", " FDPort = #{port}", " Catalog = #{client_settings[:catalog]}", " Password = \"#{password}\"", " File Retention = #{file_retention} #{file_retention_period_type}", " Job Retention = #{job_retention} #{job_retention_period_type}", " AutoPrune = #{auto_prune_human}", "}" ] + message_config end # Constructs the messages bacula resource # # @return [Array] def message_config return [] if email_recipients.empty? [ "Messages {", " Name = #{message_name}", " mailcommand = \"#{mail_command}\"", " operatorcommand = \"#{operator_command}\"", " mail = #{email_recipients.join(',')} = all, !skipped", " operator = #{settings[:operator_email]} = mount", " console = all, !skipped, !saved", " append = \"/var/log/bacula/bacula.log\" = all, !skipped", " catalog = all", "}" ] end - # Fetches the Director resource for the file-deamon configuration + # Fetches the Director resource for the file-daemon configuration # file def bacula_fd_director_config(hide_pass = true) [ 'Director {', " Name = \"#{Archiving.settings[:director_name]}\"", " Password = \"#{hide_pass ? '*********' : password }\"", '}' ].join("\n") end - # Fetches the FileDeamon resource for the file-deamon configuration - def bacula_fd_filedeamon_config + # Fetches the FileDaemon resource for the file-daemon configuration + def bacula_fd_filedaemon_config [ - 'FileDeamon {', + 'FileDaemon {', " Name = #{name}", " FDport = #{port}", ' WorkingDirectory = /var/lib/bacula', ' Pid Directory = /var/run/bacula', ' Maximum Concurrent Jobs = 10', ' FDAddress = 0.0.0.0', '}' ].join("\n") end - # Fetches the Message resource for the file-deamon configuration file + # Fetches the Message resource for the file-daemon configuration file def bacula_fd_messages_config [ 'Messages {', " Name = #{message_name}", " director = #{Archiving.settings[:director_name]} = all, !skipped, !restored", '}' ].join("\n") end # The name that the client will use for its messages def message_name "message_#{name}_#{Digest::MD5.hexdigest(created_at.to_s + name).first(10)}" end private def mail_command "#{mail_general}" << ' -t %r' << ' -u \"[Archiving]: %c %t %e\"' end def operator_command "#{mail_general}" << ' -t %r' << ' -u \"[Archiving]: Intervention needed for %j\" -m \"Intervention needed %r\"' end def mail_general command = "/usr/bin/sendEmail -f #{settings[:default_sender]}" command << " -s #{settings[:address]}:#{settings[:port]}" if settings[:user_name] && settings[:password] command << " -o tls=yes -xu #{settings[:user_name]} -xp #{settings[:password]}" end command end def settings Archiving.settings[:mail_settings] end end end diff --git a/spec/lib/configuration/host_spec.rb b/spec/lib/configuration/host_spec.rb index 18cf845..0795048 100644 --- a/spec/lib/configuration/host_spec.rb +++ b/spec/lib/configuration/host_spec.rb @@ -1,121 +1,121 @@ require 'spec_helper' describe Configuration::Host do describe '#to_bacula_config_array' do let(:host) { FactoryGirl.create(:host) } it "is a valid client directive" do expect(host.to_bacula_config_array).to include('Client {') expect(host.to_bacula_config_array).to include('}') end it "contains Address directive" do expect(host.to_bacula_config_array).to include(" Address = #{host.fqdn}") end it "contains FDPort directive" do expect(host.to_bacula_config_array).to include(" FDPort = #{host.port}") end it "contains Catalog directive" do expect(host.to_bacula_config_array). to include(" Catalog = #{ConfigurationSetting.current_client_settings[:catalog]}") end it "contains Password directive" do expect(host.to_bacula_config_array).to include(" Password = \"#{host.password}\"") end it "contains File Retention directive" do expect(host.to_bacula_config_array). to include(" File Retention = #{host.file_retention} days") end it "contains Job Retention directive" do expect(host.to_bacula_config_array). to include(" Job Retention = #{host.job_retention} days") end it "contains AutoPrune directive" do expect(host.to_bacula_config_array).to include(" AutoPrune = yes") end end describe '#baculize_config' do let!(:host) { FactoryGirl.create(:host) } let!(:fileset) { FactoryGirl.create(:fileset, host: host) } let!(:other_fileset) { FactoryGirl.create(:fileset, host: host) } let!(:schedule) { FactoryGirl.create(:schedule) } let!(:other_schedule) { FactoryGirl.create(:schedule) } let!(:enabled_job) do FactoryGirl.create(:job_template, host: host, schedule: schedule, fileset: fileset, enabled: true) end let!(:disabled_job) do FactoryGirl.create(:job_template, host: host, schedule: other_schedule, fileset: other_fileset, enabled: false) end subject { host.baculize_config } it 'includes the client\'s config' do expect(subject).to include(host.to_bacula_config_array) end it 'includes the all the job template\'s configs' do expect(subject).to include(enabled_job.to_bacula_config_array) expect(subject).to include(disabled_job.to_bacula_config_array) end it 'includes all the used schedules\'s configs' do expect(subject).to include(schedule.to_bacula_config_array) expect(subject).to include(other_schedule.to_bacula_config_array) end it 'includes all the used filesets\'s configs' do expect(subject).to include(fileset.to_bacula_config_array) expect(subject).to include(other_fileset.to_bacula_config_array) end end describe '#bacula_fd_director_config' do let!(:host) { FactoryGirl.build(:host) } subject { host.bacula_fd_director_config } it 'opens and closes a Director part' do expect(subject).to match(/^Director {$/) expect(subject).to match(/^}$/) end it 'includes the client\'s Name' do expect(subject).to match(" Name = \"#{Archiving.settings[:director_name]}\"") end it 'includes the client\'s Password' do expect(subject).to match(" Password = \"[*]+\"") end end - describe '#bacula_fd_filedeamon_config' do + describe '#bacula_fd_filedaemon_config' do let!(:host) { FactoryGirl.build(:host) } - subject { host.bacula_fd_filedeamon_config } + subject { host.bacula_fd_filedaemon_config } - it 'opens and closes a FileDeamon part' do - expect(subject).to match(/^FileDeamon {$/) + it 'opens and closes a FileDaemon part' do + expect(subject).to match(/^FileDaemon {$/) expect(subject).to match(/^}$/) end it 'includes the client\'s Port' do expect(subject).to match("FDport = #{host.port}") end it 'includes the client\'s Name' do expect(subject).to match("Name = #{host.name}") end end end