Class: RepoMate::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/repomate/cli.rb

Overview

Class for the commandline interface

Instance Method Summary collapse

Constructor Details

#initializeCli

Init



11
12
13
14
15
# File 'lib/repomate/cli.rb', line 11

def initialize
  @repomate   = Base.new
  @repository = Repository.new
  @checkpoint = Checkpoint.new
end

Instance Method Details

#choose_checkpointObject

Choose a checkpoint to restore.



78
79
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
# File 'lib/repomate/cli.rb', line 78

def choose_checkpoint
  list = @checkpoint.list

  if list.empty?
    STDERR.puts "We can't restore because we don't have checkpoints"
    exit 1
  end

  puts "\n*** Restore production links to a date below. ***
Remember: If you need to restore, the last entry might be the one you want!
Everything between the last two \"publish (-P) commands\" will be lost if you proceed!\n\n"

  list.each do |num, date|
    datetime = DateTime.parse(date)
    puts "#{num}) #{datetime.strftime("%F %T")}"
  end

  printf "\n%s", "Enter number or [q|quit] to abord: "
  input  = STDIN.gets
  number = input.to_i

  if input =~ /(q|quit)/
    puts "Aborting..."
    exit 0
  elsif list[number].nil?
    STDERR.puts "Invalid number"
    exit 1
  else
    @checkpoint.load(number)
  end
end

#choose_package(mode) ⇒ Object

Choose a package



120
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/repomate/cli.rb', line 120

def choose_package(mode)
  packages = []
  number   = 0

  @repomate.listpackages.each do |entry|
    next if entry[:category].eql?("stage")
    if mode.eql?("activate")
      file = File.join(Architecture.new(entry[:architecture], entry[:component], entry[:suitename], "dists").directory, entry[:basename])
      next if File.exists?(file)
    elsif mode.eql?("deactivate")
      next unless entry[:category].eql?("dists")
    elsif mode.eql?("remove")
      next unless entry[:category].eql?("pool")
    end
    number += 1
    packages << {
      :number       => number,
      :basename     => entry[:basename],
      :fullname     => entry[:fullname],
      :category     => entry[:category],
      :suitename    => entry[:suitename],
      :component    => entry[:component],
      :architecture => entry[:architecture],
      :controlfile  => entry[:controlfile]
    }
  end

  if number.zero?
    puts "There are no packages to #{mode}"
  else
    puts "Select a package by entering the appropriate number\n\n"

    packages.each do |entry|
      printf "%-6s%-50s%-20s%s\n", "#{entry[:number]})", entry[:controlfile]['Package'], entry[:controlfile]['Version'], "#{entry[:suitename]}/#{entry[:component]}"
    end

    printf "\n%s", "Enter number or [q|quit] to abord: "
    input  = STDIN.gets
    number = input.to_i

    if input =~ /(q|quit)/
      puts "Aborting..."
      exit 0
    else
      packages.each do |entry|
        if entry[:number].eql?(number)
          if mode.eql?("activate")
            @repomate.activate(entry)
          elsif mode.eql?("deactivate")
            @repomate.deactivate(entry, mode)
          end
        end
      end
    end
  end
end

#listpackages(options) ⇒ Object

List all packages, see cli output



111
112
113
114
115
116
117
# File 'lib/repomate/cli.rb', line 111

def listpackages(options)
  @repomate.listpackages.each do |entry|
    next unless entry[:category].eql?(options[:category]) unless options[:category].nil?
    architecture = entry[:architecture] if entry[:architecture]
    printf "%-50s%-20s%-10s%s\n", entry[:controlfile]['Package'], entry[:controlfile]['Version'], "#{entry[:category]}", "#{entry[:suitename]}/#{entry[:component]}/#{architecture}"
  end
end

#publish(options) ⇒ Object

Get’s all packages from the staging area. Packages need to be confirmed here.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/repomate/cli.rb', line 43

def publish(options)
  workload  = []
  @repomate.prepare_publish.each do |entry|
    basename  = File.basename(entry[:source_fullname])
    suitename = entry[:suitename]
    component = entry[:component]

    unless options.yes?
      printf "\n%s", "Link #{basename} to production => #{suitename}/#{component}? [y|yes|n|no]: "
      input = STDIN.gets
    end

    if options.yes? || input =~ /(y|yes)/
      workload << {
        :source_fullname      => entry[:source_fullname],
        :destination_fullname => entry[:destination_fullname],
        :component            => entry[:component],
        :suitename            => entry[:suitename],
        :architecture         => entry[:architecture]
      }
    end
  end
  
  unless workload.empty?
    @checkpoint.create
    @repomate.publish(workload)
  end
end

#save_checkpointObject

Save a checkpoint



73
74
75
# File 'lib/repomate/cli.rb', line 73

def save_checkpoint
  @checkpoint.create
end

#setup(options) ⇒ Object

Sets up the base directory structure by calling the repository class



18
19
20
21
22
23
24
25
# File 'lib/repomate/cli.rb', line 18

def setup(options)
  if options.suitename?
    @repository.create(options[:suitename], options[:component], options[:architecture])
  else
    STDERR.puts "Specify a suitename with [-s|--suitname]"
    exit 1
  end
end

#stage(options, filename) ⇒ Object

Adds a given package to the staging area by calling the base class



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/repomate/cli.rb', line 28

def stage(options, filename)
  if options.suitename?
    workload = []
    workload << {:package_fullname => filename, :suitename => options[:suitename], :component => options[:component]}

    puts "Package: #{filename} moving to stage => #{options[:suitename]}/#{options[:component]}"

    @repomate.stage(workload)
  else
    STDERR.puts "Specify a suitename with [-s|--suitname]"
    exit 1
  end
end