Class: PushConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/push-config.rb

Instance Method Summary collapse

Constructor Details

#initialize(_relPath = "instances/#{ENV['HOSTNAME']}/") ⇒ PushConfig

Returns a new instance of PushConfig.



17
18
19
20
# File 'lib/commands/push-config.rb', line 17

def initialize (_relPath = "instances/#{ENV['HOSTNAME']}/")
    @relPath = _relPath
    @log = Logger.new("#{Canzea::config[:logging_root]}/plans.log")
end

Instance Method Details

#backup(filePath) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/commands/push-config.rb', line 48

def backup(filePath)
    path = "ecosystems/#{ENV['ECOSYSTEM']}/#{@relPath}#{filePath}"

    folder = "sc"
    if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
      folder = "_working"
    end

    if File.exists? folder
        g = Git.init(folder)
    else
        if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
            g = Git.clone(ENV['ECOSYSTEM_CONFIG_GIT'], folder, :path => '.')
        else
            g = Git.init(folder)
        end
    end

    puts "Exists? #{folder}/#{path}"
    if File.exists? "#{folder}/#{path}"
        numb = 1
        bkup = "#{folder}/#{path}.archived"
        while File.exists? "#{bkup}#{numb}"
            numb = numb.next
        end
        puts "Backed up to #{bkup}#{numb}"
        File.write "#{bkup}#{numb}", File.read("#{folder}/#{path}");

        g.add("#{path}.archived#{numb}")
    end
end

#backupAndRemove(filePath) ⇒ Object



43
44
45
46
# File 'lib/commands/push-config.rb', line 43

def backupAndRemove(filePath)
    backup(filePath)
    rm(filePath)
end

#cat(filePath) ⇒ Object



22
23
24
# File 'lib/commands/push-config.rb', line 22

def cat(filePath)
    write filePath, File.read(filePath)
end

#commit(comment) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/commands/push-config.rb', line 158

def commit(comment)
    folder = "sc"
    if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
      folder = "_working"
    else
      return
    end

    begin
        count = 0


        g = Git.init(folder)

        begin
            g.chdir do
              g.status.changed.each do |file|
                    count = count.next
              end
              g.status.added.each do |file|
                    count = count.next
              end
              g.status.deleted.each do |file|
                    count = count.next
              end
            end
        rescue => exception_ignore
            count = 1
        end

        if count == 0
          log "No changes"
        else
          log "#{count} changes committed."
          g.commit(comment)

          if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
              g.push
          end
        end

        if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
          FileUtils.rm_rf(folder)
        end
    rescue => exception
        if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
          FileUtils.rm_rf(folder)
        end
        @log.error("PushConfig Failed")
        @log.error(exception.backtrace)
        @log.error(exception.to_s)
        abort()
    end
end

#cp(filePath, destPath) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/commands/push-config.rb', line 26

def cp(filePath, destPath)
    if File.directory?(filePath)
        Dir.foreach(filePath) { |x|
            if x == '.' or x == '..' or x.start_with?('.')
            else
                if File.directory?("#{filePath}/#{x}")
                    cp "#{filePath}/#{x}", "#{destPath}/#{x}"
                else
                    write "#{destPath}/#{x}", File.read("#{filePath}/#{x}")
                end
            end
        }
    else
        write destPath, File.read(filePath)
    end
end

#do(url, comment, params) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/commands/push-config.rb', line 213

def do(url, comment, params)
    begin

        # "git@#{ENV["GOGS_ADDRESS"]}:root/ecosystem.git"

        folder = "sc"
        if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
          folder = "_working"
        end

        FileUtils.rm_rf(folder)

        g = Git.clone(url, folder, :path => '.')

        params['files'].each { | f |
            file = f['file']

            path = f['path']

            FileUtils.mkdir_p File.dirname("#{folder}/#{path}")

            open("#{folder}/#{path}", 'w') { |f|
              f.puts File.read(file)
            }
            g.add("#{path}")
        }


        count = 0
        g.chdir do
          g.status.changed.each do |file|
                count = count.next
          end
          g.status.added.each do |file|
                count = count.next
          end
          g.status.deleted.each do |file|
                count = count.next
          end
        end

        if count == 0
          log "No changes"
        else
          log "#{count} changes committed."
          g.commit(comment)

          g.push
        end
    rescue => exception
        @log.error("PushConfig Failed")
        @log.error(exception.to_s)
        @log.error(exception.backtrace)
        abort()
    end
end

#log(msg) ⇒ Object



270
271
272
273
# File 'lib/commands/push-config.rb', line 270

def log (msg)
    puts msg
    @log.info(msg)
end

#rm(filePath) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/commands/push-config.rb', line 121

def rm(filePath)
    path = "ecosystems/#{ENV['ECOSYSTEM']}/#{@relPath}#{filePath}"

    begin
        folder = "sc"
        if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
          folder = "_working"
        end

        if File.exists? folder
            g = Git.init(folder)
        else
            g = Git.clone(ENV['ECOSYSTEM_CONFIG_GIT'], folder, :path => '.')
        end

        if File.directory? "#{folder}/#{path}"
            puts "DELETE DIRECTORY: #{folder}/#{path}"
            FileUtils.rmdir_p File.dirname("#{folder}/#{path}")

            g.add("#{path}")

        else
            if File.exists? "#{folder}/#{path}"
                puts "DELETE FILE: #{folder}/#{path}"
                FileUtils.rm_f "#{folder}/#{path}"
                g.remove("#{path}")
            end
        end
    rescue => exception
        @log.error("PushConfig Failed")
        @log.error(exception.to_s)
        @log.error(exception.backtrace)
        abort()
    end

end

#write(filePath, contents) ⇒ Object



80
81
82
83
84
85
86
87
88
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
# File 'lib/commands/push-config.rb', line 80

def write(filePath, contents)

    path = "ecosystems/#{ENV['ECOSYSTEM']}/#{@relPath}#{filePath}"

    begin
        folder = "sc"
        if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
          folder = "_working"
        end


        if File.exists? folder
            g = Git.init(folder)
        else
            if ENV.has_key? 'ECOSYSTEM_CONFIG_GIT'
                g = Git.clone(ENV['ECOSYSTEM_CONFIG_GIT'], folder, :path => '.')
            else
                g = Git.init(folder)
            end
        end
        FileUtils.mkdir_p File.dirname("#{folder}/#{path}")

        pth = File.expand_path "#{folder}/#{path}"

        @log.info "Writing to: #{pth}"

        open("#{folder}/#{path}", 'w') { |f|
          f.puts contents
        }

        g.add("#{path}")

    rescue => exception
        @log.error("PushConfig Failed")
        @log.error(exception.to_s)
        @log.error(exception.backtrace)
        abort()
    end

end