Class: Dodebui::Cli

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

Overview

commandline interface for dodebui

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



16
17
18
19
20
# File 'lib/dodebui/cli.rb', line 16

def initialize
  @dodebuifiles ||= ['Dodebuifile']
  @original_dir = Dir.getwd
  @distributions_sem = Mutex.new
end

Instance Attribute Details

#apt_proxyObject

Returns the value of attribute apt_proxy.



9
10
11
# File 'lib/dodebui/cli.rb', line 9

def apt_proxy
  @apt_proxy
end

#build_distributionsObject

Returns the value of attribute build_distributions.



9
10
11
# File 'lib/dodebui/cli.rb', line 9

def build_distributions
  @build_distributions
end

#source_templatesObject

Returns the value of attribute source_templates.



9
10
11
# File 'lib/dodebui/cli.rb', line 9

def source_templates
  @source_templates
end

#wdObject (readonly)

Returns the value of attribute wd.



10
11
12
# File 'lib/dodebui/cli.rb', line 10

def wd
  @wd
end

Class Method Details

.loggerObject



12
13
14
# File 'lib/dodebui/cli.rb', line 12

def self.logger
  @logger ||= Logger.new(STDOUT)
end

Instance Method Details

#buildObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dodebui/cli.rb', line 118

def build
  threads = []
  @distributions.each do |dist|
    threads << Thread.new do
      begin
        dist.build.build
      rescue => e
        logger.warn("Failed building on image '#{dist.image_name}': #{e}")
        @distributions_sem.synchronize do
          @distributions -= [dist]
        end
      end
    end
  end
  # wait for all threads
  threads.each(&:join)
end

#dodebuifile?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'lib/dodebui/cli.rb', line 22

def dodebuifile?
  @dodebuifiles.each do |fn|
    return fn if File.exist?(fn)
  end
  nil
end

#ensure_images_updatedObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dodebui/cli.rb', line 91

def ensure_images_updated
  # ensure images are up to date
  threads = []
  @distributions.each do |dist|
    threads << Thread.new do
      begin
        dist.ensure_image_updated
      rescue => e
        logger.warn(
          "Failed ensuring a updated image '#{dist.image_name}': #{e}"
        )
        @distributions_sem.synchronize do
          @distributions -= [dist]
        end
      end
    end
  end
  # wait for all threads
  threads.each(&:join)
end

#find_dodebuifile_locationObject

:nodoc:



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

def find_dodebuifile_location # :nodoc:
  here = Dir.pwd
  until (fn = dodebuifile?)
    Dir.chdir('..')
    return nil if Dir.pwd == here
    here = Dir.pwd
  end
  [fn, here]
ensure
  Dir.chdir(@original_dir)
end

#load_dodebiufileObject



41
42
43
44
45
46
47
48
49
# File 'lib/dodebui/cli.rb', line 41

def load_dodebiufile
  dodebuifile, location = find_dodebuifile_location
  fail 'No Dodebuifile found' if dodebuifile.nil?
  @dodebuifile = File.join(location, dodebuifile)
  @wd = location
  Cli.logger.info("Working directory #{@wd}")
  Cli.logger.info("Config file #{@dodebuifile}")
  load_dodebiufile_raw @dodebuifile
end

#load_dodebiufile_raw(path) ⇒ Object



51
52
53
54
55
56
# File 'lib/dodebui/cli.rb', line 51

def load_dodebiufile_raw(path)
  File.open(path, 'r') do |infile|
    code = infile.read
    eval(code) # rubocop:disable Lint/Eval
  end
end

#loggerObject



80
81
82
# File 'lib/dodebui/cli.rb', line 80

def logger
  Cli.logger
end

#prepare_distributions(distributions = []) ⇒ Object



84
85
86
87
88
89
# File 'lib/dodebui/cli.rb', line 84

def prepare_distributions(distributions = [])
  @distributions = distributions.map do |name|
    Distribution.new(name, self)
  end
  ensure_images_updated
end

#prepare_sourcesObject



112
113
114
115
116
# File 'lib/dodebui/cli.rb', line 112

def prepare_sources
  @distributions.each do |dist|
    dist.build.source
  end
end

#runObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dodebui/cli.rb', line 58

def run
  Cli.logger.info("Initializing dodebui #{VERSION}")

  load_dodebiufile

  test_docker

  prepare_distributions build_distributions

  prepare_sources

  build
end

#test_dockerObject



72
73
74
75
76
77
78
# File 'lib/dodebui/cli.rb', line 72

def test_docker
  Docker.options[:read_timeout] = 3600
  data = Docker.version
  Cli.logger.info(
    "Connecting to Docker server successful (version #{data['Version']})"
  )
end