Class: AptControl::CLI::Watch

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/apt_control/cli/watch.rb

Instance Method Summary collapse

Methods included from Common

#apt_site, #build_archive, #control_file, #each_package_state, #logger, #notifier, #notify, #validate_config!

Instance Method Details

#daemonize!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/apt_control/cli/watch.rb', line 32

def daemonize!
  pidfile = options[:pidfile]

  if pidfile && File.exists?(pidfile)
    $stderr.puts("pidfile exists, not starting")
    exit 1
  end

  if uid = options[:setuid]
    logger.info("setting uid to #{uid}")
    begin
      Process::Sys.setuid(uid)
    rescue Errno::EPERM => e
      raise Climate::ExitException, "Could not setuid with #{uid}"
    end
  end

  pid = fork
  exit 0 unless pid.nil?

  File.open(pidfile, 'w') {|f| f.write(Process.pid) } if pidfile

  at_exit { File.delete(pidfile) if File.exists?(pidfile) } if pidfile
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/apt_control/cli/watch.rb', line 18

def run
  validate_config!

  # hit these before we daemonize so we don't just background and die
  apt_site
  control_file
  build_archive

  daemonize! if options[:daemonize]

  start_watching
end

#start_watchingObject



57
58
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
88
89
90
91
92
93
# File 'lib/apt_control/cli/watch.rb', line 57

def start_watching
  # update the all the rules if the control file changes
  Thread.new { control_file.watch { notify "Control file reloaded" } }

  notify("Watching for new packages in #{build_archive.dir}")
  build_archive.watch do |package, new_version|
    notify("new package: #{package.name} at #{new_version}")

    updated = control_file.distributions.map do |dist|
      rule = dist[package.name] or next
      included = apt_site.included_version(dist.name, package.name)

      if rule.upgradeable?(included, [new_version])
        if options[:noop]
          notify("package #{package.name} can be upgraded to #{new_version} on #{dist.name} (noop)")
        else
          # FIXME error handling here, please
          begin
            apt_site.include!(dist.name, build_archive.changes_fname(rule.package_name, new_version))
            notify("included package #{package.name}-#{new_version} in #{dist.name}")
          rescue => e
            notify("Failed to include package #{package.name}-#{new_version}, check log for more details")
            logger.error("failed to include package #{package.name}")
            logger.error(e)
          end
        end
        dist.name
      else
        nil
      end
    end.compact

    if updated.size == 0
      notify("package #{package.name} could not be updated on any distributions")
    end
  end
end