Class: LogicalConstruct::Target::FlightDeck

Inherits:
Mattock::Tasklib
  • Object
show all
Includes:
Mattock::CommandLineDSL
Defined in:
lib/logical-construct/target/flight-deck.rb

Defined Under Namespace

Classes: DaemonizedResolutionServerTask, ResolutionServerTask, UniqueProcessTask

Instance Method Summary collapse

Instance Method Details

#defineObject



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
212
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/logical-construct/target/flight-deck.rb', line 165

def define
  in_namespace do
    UniqueProcessTask.define_task(:unique_process)

    namespace :server do
      desc "Run resolution server synchronously"
      ResolutionServerTask.define_task(:run) do |run|
        copy_settings_to(run)
      end

      DaemonizedResolutionServerTask.define_task(:daemonized) do |daemon|
        copy_settings_to(daemon)
      end

      task :report_started => :daemonized do
        puts "** Started resolution server"
      end

      task :deliver_manifest => :report_started do
        unless manifest_path.nil?
          puts "*** the --manifest_path option is a placeholder for the real feature ***"
        end
      end
    end

    namespace :plans do
      task :wait_for_server => "server:deliver_manifest" do
        client = NodeClient.new
        client.node_url = "http://localhost:30712/"

        puts "** Waiting for ground control resolution"
        current_state = nil
        until (state = client.state) == "resolved"
          if current_state != state
            puts "** Current state: #{state}"
          end
          current_state = state
          sleep 1
        end
        puts "** Server ready - proceeding..."
      end

      desc "Check that current plans are received and unpack them for implementation"
      task :unpack => 'unpack:finished'

      namespace :unpack do
        desc "Remove old versions of the the live plans directory"
        task :clean_up do
          retain = []
          [plans_live.pathname, plans_temp.pathname].each do |path|
            begin
              retain << path.realpath
            rescue Errno::ENOENT
            end
          end

          if(plan_dirs.pathname.exist?)
            plan_dirs.pathname.children(true).each do |dir|
              next if retain.include?(dir.realpath)
              cmd("rm", "-rf", dir.to_s).run
            end
          end
        end

        directory plan_dirs.absolute_path

        file plans_temp.absolute_path => plan_dirs.absolute_path do
          require 'tmpdir'

          dir = Dir::mktmpdir(nil, plan_dirs.absolute_path)
          cmd("ln") do |link|
            link.options << "-sfnT"
            link.options << dir
            link.options << plans_temp.absolute_path
          end.must_succeed!
        end

        task :make_temp_live do
          cmd("mv") do |mv_t|
            mv_t.options << "-T"
            mv_t.options << plans_temp.absolute_path
            mv_t.options << plans_live.absolute_path
          end.must_succeed!
        end

        #This task creates other tasks here and now for unpacking the
        #current plans - we don't know until now which plan archives
        #we're installing
        #We could sort of use a rule, but we don't know anything about
        #the files in the archives.
        task :all => :wait_for_server do
          complete_task = task :complete

          Pathname.new(current.absolute_path).each_child(true) do |archive_file|
            name = archive_file.basename.sub(/[.].*\Z/,'').to_s
            temp_plan = plans_temp.pathname.join(name)

            namespace(name) do
              unpack_task = UnpackTarballTask.define_task(:unpack => plans_temp.absolute_path) do |unpack|
                unpack.unpacked_parent.absolute_path = plans_temp.absolute_path
                unpack.archive_parent.absolute_path = current.absolute_path
                unpack.basename = name
              end
              unpack_task.create_target_dependencies

              plan_file = unpack_task.unpacked_dir.pathname.join("plan.rake")
              file plan_file

              task :install => unpack_task.target_files do
                source_path = unpack_task.unpacked_dir.pathname
                if (install_rake = source_path.join("plan.rake")).exist?
                  ruby_libs = [source_path.join("lib"), ENV["RUBYLIB"]]
                  (cmd("cd", source_path) &
                   cmd("rake", "--rakefile", install_rake, "#{name}:install").set_env("RUBYLIB", ruby_libs.join(":"))
                  ).must_succeed!
                end
              end
            end

            task :complete => "#{name}:install"
          end

          complete_task.invoke
        end

        task :tidy_up => [:make_temp_live, :clean_up]
        task :finished => [:wait_for_server, :tidy_up, :make_temp_live]
        task :make_temp_live => :all
      end

      task :implement => :unpack do
        libpath = []
        gempath = []
        rake_modules = []


        plans_live.pathname.each_child(false) do |plan_path|
          if (libdir = plans_live.pathname + plan_path + "lib").directory?
            libpath << libdir
          end

          if (plan_module = plans_live.pathname + plan_path + "plan.rake").file?
            rake_modules << plan_path + "plan"
          end
        end

        rake_command = cmd("rake") do |rake|
          rake.options += ["--libdir", plans_live.pathname]
          rake_modules.each do |mod|
            rake.options += ["--require", mod]
          end

          rake.options << "--rakefile " + File::expand_path("../Implement.rake", __FILE__)

          #rake.options << "--trace"
          rake.options += plan_options

          rake.options += top_level_tasks
          rake.command_environment["RUBYLIB"] = libpath.join(":")
        end

        puts "*** Beginning Implementation"
        rake_command.replace_us
      end

      task :wait_for_server => :unique_process
      file plans_temp.absolute_path => :unique_process
      task :implement => :unique_process
    end

    task :implement => 'plans:implement'
    task :start_server => 'server:daemonized'
  end
end

#resolve_configurationObject



159
160
161
162
163
# File 'lib/logical-construct/target/flight-deck.rb', line 159

def resolve_configuration
  self.absolute_path = Pathname.pwd.join($0).dirname.dirname.to_s
  resolve_paths
  super
end