Class: GatgetSynchronization

Inherits:
Gat::Base show all
Defined in:
lib/gatgets/synchronization/synchronization.rb

Direct Known Subclasses

MyRsyncScript

Instance Attribute Summary

Attributes inherited from Gat::Base

#arguments, #config, #debug, #flags, #logger, #name, #onfail_error, #operation, #path, #times, #variables, #verbose

Instance Method Summary collapse

Methods inherited from Gat::Base

#execute, #get_dependence_value, #get_dependence_variable, #get_element_config, #get_flag, #initialize, #set_dependence_variable, #set_flag

Methods included from Gat::Email

#create_and_deliver_email, #new_email, #send_email

Methods included from Gat::Launcher

included

Methods included from Gat::Help

#gatget_help, #returning

Methods included from Gat::Debug

#get_debug_levels, #print_debug_msg

Methods included from Gat::Checks

#check_enough_space, #check_false, #check_file_exists, #check_file_not_exists, #check_i_am_running, #check_true, #exit_if_i_am_running, #need_arguments

Constructor Details

This class inherits a constructor from Gat::Base

Instance Method Details

#check_host_definedObject



32
33
34
35
36
37
38
# File 'lib/gatgets/synchronization/synchronization.rb', line 32

def check_host_defined
  allhosts = `cut -s -f 2 /etc/hosts`
  required_host = get_dependence_variable("remote_host")
  if not allhosts.split("\n").include?( required_host )
    raise Gat::GatgetException.new( "Host #{required_host} not defined in /etc/hosts", "check_host_defined" )
  end
end

#exec_sync_mutiple_hostsObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gatgets/synchronization/synchronization.rb', line 117

def exec_sync_mutiple_hosts
  hosts = get_dependence_value("static", "all_hosts")
  while hosts.any?
    hosts.each do |host|
      opname = "sync_#{host}"
      begin
        a = Gat::Operation.new(opname, self.config['operations'][opname], self)
      rescue
        raise Gat::GatgetConfigException.new("Operation '#{opname}' not defined in yml", "exec_sync_mutiple_hosts")
      end
      a.execute
      backup_was_running = self.get_flag("flag_remote_backup_running")
      if not backup_was_running
        status = a.get_action("exec_remote_rsync").status
        p status
        if status == "ok"
          hosts.delete host
        end
      end
    end
    sleep 100
  end
end

#notify_endObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gatgets/synchronization/synchronization.rb', line 66

def notify_end
  st_notify_email_ended = get_dependence_value("static", "notify_email_ended")
  if st_notify_email_ended && st_notify_email_ended["send_to"]
      to = st_notify_email_ended
  end
  subject = st_notify_email_ended["subject"]
  body = st_notify_email_ended["body"]
  if to.nil?
    if self.config["email_config"]
      to = self.config["email_config"]["send_to"]
    else
      raise Gat::GatgetConfigException.new("Default email config[send_to] is empty at gatget.config['email_config']", "run_output_email")
    end
  end
  subject = "Rsync ended" if subject.nil?
  body = "Define your own body in static.notify_email_ended.body" if body.nil?

  full_subject = "[GAT] [#{ self.class.name }] [#{ self.operation.name.camelize }] #{subject}"
  create_and_deliver_email(to,
     { 'subject' => full_subject,
       'body' => body },
     self.config["email_config"]
  )
end

#notify_startObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gatgets/synchronization/synchronization.rb', line 40

def notify_start
  st_notify_email_started = get_dependence_value("static", "notify_email_started")
  if st_notify_email_started && st_notify_email_started["send_to"]
      to = st_notify_email_started
  end
  subject = st_notify_email_started["subject"]
  body = st_notify_email_started["body"]
  if to.nil?
    if self.config["email_config"]
      to = self.config["email_config"]["send_to"]
    else
      raise Gat::GatgetConfigException.new("Default email config[send_to] is empty at gatget.config['email_config']", "run_output_email")
    end
  end
  subject = "Rsync started" if subject.nil?
  body = "Define your own body in static.notify_email_started.body" if body.nil?

  full_subject = "[GAT] [#{ self.class.name }] [#{ self.operation.name.camelize }] #{subject}"
  create_and_deliver_email(to,
     { 'subject' => full_subject,
       'body' => body },
     self.config["email_config"]
  )
end

#onfailObject

send rescue email will send a email for notification errors at do_backup operation



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gatgets/synchronization/synchronization.rb', line 94

def onfail

  unless self.config['email_config']
    raise Gat::GatgetConfigException.new("Default email config is empty at gatget.config['email_config']", "synchronization:onfail")
  end

  unless self.config['email_config']['send_to']
    raise Gat::GatgetConfigException.new("Default email config 'send_to' is empty at gatget.config['email_config']", "run_output_email")
  end

  email_subject = "[GAT] [#{ self.class.name }] [#{ self.operation.name.camelize }] [FAILED]"


  email_body    =  "Gatget #{ self.class.name } was stopped by raising of #{ self.onfail_error.class } at #{ Time.now }\n\n"
  email_body    << "#{ onfail_error.exit_error }"
  email_body    << "\n\n Errors log:  \n\n #{self.logger.entries.select{ |e| e.global_type == 'error' }.map{ |e| e.inspect }.join("\n") }\n\n"
  email_body    << "\n\n Warning log: \n\n #{self.logger.entries.select{ |e| e.global_type == 'warning' }.map{ |e| e.inspect }.join("\n") }\n\n"
  email_body    << "\n\n Whole log:   \n\n #{self.logger.entries.map{ |e| e.inspect }.join("\n") }"

  self.create_and_deliver_email(self.config['email_config']['send_to'], { 'subject' => email_subject, 'body' => email_body }, self.config['email_config'])

end

#set_parameters_for_my_hostObject



141
142
143
144
145
146
# File 'lib/gatgets/synchronization/synchronization.rb', line 141

def set_parameters_for_my_host
  set_dependence_variable( "remote_user", "backup" )
  set_dependence_variable( "remote_folder", "/home/backup/" )
  set_dependence_variable( "remote_host", "my_host" )
  set_dependence_variable( "local_folder",  "/home/backup/rsyncs/my_host" )
end

#setflag_remote_backup_runningObject



26
27
28
29
30
# File 'lib/gatgets/synchronization/synchronization.rb', line 26

def setflag_remote_backup_running
  a = Gat::Action::ShellCommand.new('check_remote_backup_status', self.config['actions']['check_remote_backup_status'], self.operation)
  a.execute
  set_flag("flag_remote_backup_running", a.output.downcase == "running\n" )
end