diff --git a/.gitignore b/.gitignore index 5678ce7..c30246e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,26 +1,29 @@ # See https://help.github.com/articles/ignoring-files for more about ignoring files. # # If you find yourself ignoring temporary files generated by your text editor # or operating system, you probably want to add a global ignore instead: # git config --global core.excludesfile '~/.gitignore_global' # Ignore bundler config. /.bundle # Ignore the default SQLite database. /db/*.sqlite3 /db/*.sqlite3-journal /db/*.local /db/database.yml # Ignore all logfiles and tempfiles. /log/*.log /tmp *.swp + +# Ignore config files config/database.yml config/beanstalk.yml config/secrets.yml config/bconsole.conf +config/ssh.yml .ruby-version .rspec diff --git a/config/ssh.yml.sample b/config/ssh.yml.sample new file mode 100644 index 0000000..9de0078 --- /dev/null +++ b/config/ssh.yml.sample @@ -0,0 +1,5 @@ +development: + key_file: /home/stratos/.ssh/bacukey + username: root + host: ser-baculas.lan + path: /etc/bacula/clientdefs/ diff --git a/lib/bacula_handler.rb b/lib/bacula_handler.rb new file mode 100644 index 0000000..1c75637 --- /dev/null +++ b/lib/bacula_handler.rb @@ -0,0 +1,80 @@ +class BaculaHandler + require 'net/scp' + + attr_accessor :host, :tempfile + + # Initializes a BaculaHandler instance. + # + # Sets `host` and `template` attributes. + # + # @param host[Host] A the host instance the the bacula handler will act upon + def initialize(host) + @host = host + @tempfile = get_config_file + end + + # Deploys the host's config to the bacula director by + # + # * uploading the configuration + # * reloadind the bacula director + # + # Updates the host's status accordingly + # + # @return [Boolean] false if something went wrong + def deploy_config + return false unless send_config + if reload_bacula + host.set_deployed + else + host.dispatch || host.redispatch + end + end + + def get_config_file + file = Tempfile.new(host.name) + file.chmod(0666) + file.write host.baculize_config.join("\n") + file.close + file + end + + def send_config + begin + Net::SCP.upload!( + ssh_settings[:host], + ssh_settings[:username], + tempfile.path, + ssh_settings[:path] + host.name + '.conf', + ssh: { keys: [ssh_settings[:key_file]] } + ) + rescue + return false + end + true + end + + def reload_bacula + command = "echo \"reload quit\" | #{bconsole}" + exec_with_timeout(command, 2) + end + + def exec_with_timeout(command, sec) + begin + Timeout::timeout(sec) do + `#{command}` + end + rescue + return false + end + true + end + + def bconsole + "bconsole -c #{Rails.root}/config/bconsole.conf" + end + + def ssh_settings + @ssh_settings ||= YAML::load(File.open("#{Rails.root}/config/ssh.yml"))[Rails.env]. + symbolize_keys + end +end