Module: Forj::Boot

Defined in:
lib/boot.rb,
lib/boot.rb,
lib/boot.rb

Overview

This module provides the behavior to boot your forge

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#accountObject

Returns the value of attribute account.



27
28
29
# File 'lib/boot.rb', line 27

def 
  @account
end

Class Method Details

.boot(blueprint, on_or_name, deprecated_name, options) ⇒ Object

Boot process



107
108
109
110
111
112
113
114
115
116
117
118
119
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
# File 'lib/boot.rb', line 107

def self.boot(blueprint, on_or_name, deprecated_name, options)
  @account = Lorj::Account.new(options[:config])

  name = deprecated_name?(blueprint, on_or_name, deprecated_name[0],
                          deprecated_name[1], deprecated_name[2])

  PrcLib.fatal(1, "instance name '%s' not supported. Support only lower" \
               ' case, numeric and dash caracters.',
               name) unless /^[\d[[:lower:]]-]+$/ =~ name

  # Options are added if they are set.
  # Otherwise, get will retrieve the default value.

  @account[:account_name] = options[:account_name] if options[:account_name]

  unless @account.ac_load @account[:account_name]
    PrcLib.fatal(1, "Account '%s' not loaded. You need to call "\
                 '`forj setup %s [provider]` to use this account.',
                 @account[:account_name], @account[:account_name])
  end

  options[:tb_path] = nil if options.key?(:test_box) &&
                             !options.key?(:tb_path)
  options_map = { :infra          => :infra_repo,
                  :key_name       => :keypair_name,
                  :key_path       => :keypair_path,
                  :security_group => :security_group,
                  :image_name     => :image_name,
                  :maestro_flavor => :flavor,
                  :bp_flavor      => :bp_flavor,
                  :maestro_repo   => :maestro_repo,
                  :branch         => :branch,
                  :test_box       => :test_box,
                  :tb_path        => :test_box_path,
                  :ca_root_cert   => :ca_root_cert,
                  :extra_metadata => :extra_metadata,
                  :webproxy       => :webproxy,
                  :disable_lorj   => :lorj_disabled
                }

  load_options(options, options_map) { |k, v| complete_boot_options(k, v) }

  validate_keypath(options)

  # o_cloud = get_o_cloud(o_forj_account)
  o_cloud = Forj::CloudConnection.connect(@account)

  install_blueprint?(blueprint, name)
  PrcLib.high_level_msg("Preparing your forge '%s'.Please be patient. "\
                        "more output in '%s'\n",
                        @account[:instance_name],
                        PrcLib.log_file)

  o_cloud.create(:forge,
                 :server_name    => "maestro.#{@account[:instance_name]}",
                 :image_name     => @account['maestro#image_name'],
                 :flavor_name    => @account['maestro#flavor_name'],
                 :network_name   => @account['maestro#network_name'],
                 :security_group => @account['maestro#security_group'],
                 :ports          => @account[:ports])
end

.ca_root_file_detect(param) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/boot.rb', line 276

def self.ca_root_file_detect(param)
  res_found = param.match(/^(.*)#(.*)$/)

  if res_found
    cert_file = File.expand_path(res_found[1])
  else
    cert_file = File.expand_path(param)
  end

  unless File.readable?(cert_file)
    PrcLib.error("Unable to read the Root Certificate file '%s'", cert_file)
    return nil
  end
  param
end

.complete_boot_options(key, value) ⇒ Object

Take care of special options cases for boot.



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/boot.rb', line 175

def self.complete_boot_options(key, value)
  case key
  when :test_box
    value = tb_repo_detect(value)
  when :tb_path
    value = tb_bin_detect(value)
  when :ca_root_cert
    value = ca_root_file_detect(value)
  end
  value
end

.deprecated_name?(blueprint, on_or_name, old_accountname, as, old_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/boot.rb', line 29

def self.deprecated_name?(blueprint, on_or_name,
    old_accountname, as,
    old_name
                         )
  # depreciated: <BluePrint> on <AccountName> as <InstanceName>
  if old_accountname && as && old_name
    msg = format(
      "The syntax `forj boot '%s' on '%s' as '%s'`" \
    " is depreciated. \nUse `forj boot '%s' '%s' ",
      blueprint, old_accountname, old_name, blueprint, old_name
    )

    if .get(:account_name) == old_accountname
      PrcLib.warning('%s` instead.', msg)
    else
      PrcLib.warning("%s -a '%s'` instead.", msg, old_accountname)
    end
    name = old_name
    @account.set(:account_name, old_accountname)
  else
    name = on_or_name
  end
  name
end

.install_blueprint?(blueprint, name) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/boot.rb', line 90

def self.install_blueprint?(blueprint, name)
  @account[:instance_name] = name

  if blueprint == 'maestro'
    PrcLib.info("Starting boot process of '%s'. No blueprint requested.",
                @account[:instance_name])
  else
    @account[:blueprint] = blueprint
    PrcLib.info("Starting boot process of '%s' with blueprint '%s'.",
                @account[:instance_name], @account[:blueprint])
  end
end

.load_options(options, options_map) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/boot.rb', line 54

def self.load_options(options, options_map)
  options_map.each do |opt_key, ac_key|
    if options.key?(opt_key)
      value = yield(opt_key, options[opt_key])
      @account.set(ac_key, value) unless value.nil?
    end
  end
end

.tb_bin_detect(tb_path) ⇒ Object

function to detect if test-box.sh is runnable

It returns the script to execute.



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
# File 'lib/boot.rb', line 240

def self.tb_bin_detect(tb_path)
  tb_path = ENV['TEST_BOX'] if tb_path.nil?
  tb_path = File.expand_path(tb_path) unless tb_path.nil?

  script = 'test-box.sh'
  if tb_path && File.directory?(tb_path)
    script_found = tb_check_bin(tb_path)
    script = File.expand_path(File.join(tb_path, script))
    if script_found.nil?
      PrcLib.error("Test-box: '%s' is not a valid runnable script. "\
                   'test-box is disabled.', script)
      return nil
    end
    PrcLib.debug("tb_repo_detect: FOUND #{script_found}")
    return script_found
  end

  script_found = nil

  ENV['PATH'].split(':').each do |path|
    script_found = tb_check_bin(path)
    break unless script_found.nil?
  end
  PrcLib.debug("tb_repo_detect: FOUND #{script_found}")

  script_found
end

.tb_check_bin(tb_path) ⇒ Object

Script to check the bin and path



269
270
271
272
273
274
# File 'lib/boot.rb', line 269

def self.tb_check_bin(tb_path)
  script = 'test-box.sh'
  script = File.expand_path(File.join(tb_path, script))
  return script if File.executable?(script)
  nil
end

.tb_repo_detect(paths) ⇒ Object

Function to check the repository path passed.



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
# File 'lib/boot.rb', line 188

def self.tb_repo_detect(paths)
  res = {}
  paths.each do |path|
    PrcLib.debug("tb_repo_detect: checking #{path}")
    cmd = <<-CMD
cd "#{path}"
if [ $? -ne 0 ]
then
  exit 1
fi
git rev-parse --show-toplevel 2>/dev/null 1>&2
if [ $? -ne 0 ]
then
   exit 1
fi
REPO_TO_ADD="$(LANG= git remote show origin -n |
                 awk '$1 ~ /Fetch/ { print $3 }')"
if [ "$REPO_TO_ADD" = "" ]
then
   exit 1
fi
echo $REPO_TO_ADD
pwd
    CMD
    cmd_res = `#{cmd}`.split
    # For any reason, $CHILD_STATUS is empty, while $? is not.
    # Ruby bug. tested with:
    # ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux]
    # rubocop: disable Style/SpecialGlobalVars
    unless $?.exitstatus == 0
      PrcLib.warning("tb_repo_detect: #{path} seems not to be a GIT "\
                     "repo.\n"\
                     "#{cmd_res.join("\n")}")
      next
    end
    # rubocop: enable Style/SpecialGlobalVars
    repo_found = cmd_res[0].match(%r{.*/(.*)(.git)?})
    unless repo_found
      PrcLib.warning("tb_repo_detect: Unable to find the repo path.\n"\
                     "#{cmd_res.join("\n")}")

      next
    end
    res[repo_found[1]] = cmd_res[1]
    PrcLib.debug("tb_repo_detect: FOUND #{cmd_res[1]}")
  end
  res
end

.validate_keypath(options) ⇒ Object



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
# File 'lib/boot.rb', line 63

def self.validate_keypath(options)
  if options[:key_path]
    m_found = options[:key_path].match(/^(.*)(\.pub)?$/)
    if m_found
      key_path = File.expand_path(m_found[1])
      if m_found[2] && !(File.exist?(
        File.expand_path(m_found[1] + m_found[2])
      ))
        PrcLib.fatal(
          1,
          "'%s' is not a valid keypair files." \
          ' At least the public key (.pub) is have to exist.',
          key_path
        )
      end
      @account.set(:keypair_path, key_path)
    else
      PrcLib.fatal(
        1,
        "'%s' is not a valid keypair files." \
        'At least the public key (.pub) is have to exist.',
        key_path
      )
    end
  end
end