Class: Gitchefsync::ScheduleSync

Inherits:
Object
  • Object
show all
Defined in:
lib/gitchefsync/schedule.rb

Instance Method Summary collapse

Constructor Details

#initializeScheduleSync

Returns a new instance of ScheduleSync.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gitchefsync/schedule.rb', line 21

def initialize()
  options = Gitchefsync.options
  config = Gitchefsync.configuration
  
  @lock_filename = config['lock_filename'] || 'sync.lock'
  @lock_timeout = config['lock_timeout'] || 10
  @lock_timeout = @lock_timeout.to_i
  
  @master_sync_timeout = config['master_sync_timeout'] || 600
  @master_sync_timeout = @master_sync_timeout.to_i
        
  @sous_rsync_user = config['sous_rsync_user'] || 'chefsync'
  @sous_rsync_host = config['sous_rsync_host'] || ''
  @sous_rsync_src = config['sous_rsync_src'] || config['stage_dir'] || '/opt/gitchefsync/staging/'
  @sous_rsync_dest = config['sous_rsync_dest'] || 
                     File.join(File::SEPARATOR, config['stage_dir'].split(File::SEPARATOR)[1..-2]) || 
                     '/opt/gitchefsync/'
  @sous_rsync_options = config['sous_rsync_options'] || '-ar --delete'
  @sous_rsync_excludes = config['sous_rsync_excludes'] || '.chef .snapshot'
  
  @sous_sync_timeout = config['sous_sync_timeout'] || 600
  @sous_sync_timeout = @sous_sync_timeout.to_i 
end

Instance Method Details

#cleanTmpObject

Due to ridley bug (4.0.0+ possibly earlier) -clean the tmp directories takes current “Date” and cleans up



140
141
142
143
144
# File 'lib/gitchefsync/schedule.rb', line 140

def cleanTmp
  ts_str = "/tmp/d" + Date.today.strftime("%Y%m%d") + "-*"
  Gitchefsync.logger.info "clean up of #{ts_str}"
  FS.cmdNoError "sudo rm -fr #{ts_str}"
end

#masterObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gitchefsync/schedule.rb', line 59

def master
  lock_file = obtainExclusiveLock
  
  begin
    Timeout::timeout(@master_sync_timeout) do
      Gitchefsync.logger.info "event_id=master_sync_starting"
  
      #Setup and check Gitlab API endpoint
      Gitlab.endpoint = 'http://gitlab.rim.net/api/v3'
      Gitchefsync.checkGit
  
      Gitchefsync.syncEnv
      Gitchefsync.syncCookbooks
      Gitchefsync.reconcile
      cleanTmp()
      Gitchefsync.logger.info "event_id=master_sync_completed"
      #notification
      Gitchefsync.notifyFromAudit()
      # MAND-615 - This file will signal its ok for this directory to be sous sync target
      File.write(File.join(@sous_rsync_src, "master_sync_completed"), "")
    end
  rescue Timeout::Error
    Gitchefsync.logger.fatal "event_id=master_sync_timed_out:master_sync_timeout=#{@master_sync_timeout}"
    exit 1
  rescue => e
    Gitchefsync.logger.error "event_id=caught_exception:msg=#{e.message}"
  end
  lock_file.close
end

#obtainExclusiveLockObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitchefsync/schedule.rb', line 45

def obtainExclusiveLock 
  Gitchefsync.logger.info "event_id=attempt_to_lock_file:lock_filename=#{@lock_filename}"
  lock_file = File.open(@lock_filename, File::RDWR|File::CREAT, 0644)
  
  begin
    Timeout::timeout(@lock_timeout) { lock_file.flock(File::LOCK_EX) }
  rescue
    Gitchefsync.logger.fatal "event_id=unable_to_lock_file:lock_filename=#{@lock_filename}"
    exit 1
  end
  
  lock_file
end

#sousObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gitchefsync/schedule.rb', line 89

def sous
  lock_file = obtainExclusiveLock
  
  begin
    Timeout::timeout(@sous_sync_timeout) do
      Gitchefsync.logger.info "event_id=sous_sync_starting"
      
      exclude = ""
      @sous_rsync_excludes.split(" ").each do |pattern|
        exclude = "#{exclude} --exclude #{pattern}"
      end
      
      if @sous_rsync_host.empty?
        Gitchefsync.logger.fatal "event_id=sous_rsync_host_not_configured"
        exit 1
      end
      
      master_sync_completed = File.join(@sous_rsync_src, "master_sync_completed")
      master_sync_completed_cmd = "ssh #{@sous_rsync_user}@#{@sous_rsync_host} ls #{master_sync_completed} 2>/dev/null"
      Gitchefsync.logger.info "event_id=check_master_sync_completed:cmd=#{master_sync_completed_cmd}"
      master_sync_completed_stdout = FS.cmd "#{master_sync_completed_cmd}"
      
      # MAND-615 - Do not perform an rsync on #{master_sync_completed} target if empty.
      # Avoid doing rsync command and staged cookbook/env upload in situation where master 
      # chef server was re-instantiated and master sync has yet to run once.
      if master_sync_completed_stdout.empty?
        Gitchefsync.logger.fatal "event_id=missing_master_sync_completed_file:master_sync_completed=#{master_sync_completed}"
        exit 1
      end
        
      rsync_cmd = "rsync #{@sous_rsync_options} #{exclude} #{@sous_rsync_user}@#{@sous_rsync_host}:#{@sous_rsync_src} #{@sous_rsync_dest} 2>/dev/null"
      Gitchefsync.logger.info "event_id=execute_rsync:cmd=#{rsync_cmd}"
      FS.cmd "#{rsync_cmd}"
  
      Gitchefsync.stagedUpload
      Gitchefsync.syncEnv
      Gitchefsync.reconcile
      cleanTmp()
      Gitchefsync.logger.info "event_id=sous_sync_completed"
    end
  rescue Timeout::Error
    Gitchefsync.logger.fatal "event_id=sous_sync_timed_out:sous_sync_timeout=#{@sous_sync_timeout}"
    exit 1
  rescue => e
    Gitchefsync.logger.error "event_id=caught_exception:msg=#{e.message}"
  end
  lock_file.close
end