Class: Beaker::Subcommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/beaker/subcommand.rb

Constant Summary collapse

SubcommandUtil =
Beaker::Subcommands::SubcommandUtil

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Subcommand

Returns a new instance of Subcommand.



10
11
12
13
14
15
16
# File 'lib/beaker/subcommand.rb', line 10

def initialize(*args)
  super
  FileUtils.mkdir_p(SubcommandUtil::CONFIG_DIR)
  FileUtils.touch(SubcommandUtil::SUBCOMMAND_OPTIONS) unless SubcommandUtil::SUBCOMMAND_OPTIONS.exist?
  FileUtils.touch(SubcommandUtil::SUBCOMMAND_STATE) unless SubcommandUtil::SUBCOMMAND_STATE.exist?
  @cli = Beaker::CLI.new
end

Instance Method Details

#destroyObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/beaker/subcommand.rb', line 200

def destroy()
  if options[:help]
    invoke :help, [], ["destroy"]
    return
  end

  state = YAML::Store.new(SubcommandUtil::SUBCOMMAND_STATE)
  unless state.transaction { state['provisioned']}
    SubcommandUtil.error_with('Please provision an environment')
  end

  @cli.parse_options
  @cli.options[:provision] = false
  @cli.initialize_network_manager
  @cli.network_manager.cleanup

  state.transaction {
    state.delete('provisioned')
  }
end

#exec(resource = nil) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
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
# File 'lib/beaker/subcommand.rb', line 154

def exec(resource=nil)
  if options[:help]
    invoke :help, [], ["exec"]
    return
  end

  @cli.parse_options
  @cli.initialize_network_manager

  if !resource
    @cli.execute!
    return
  end

  beaker_suites = [:pre_suite, :tests, :post_suite, :pre_cleanup]

  if Pathname(resource).exist?
    # If we determine the resource is a valid file resource, then we empty
    # all the suites and run that file resource in the tests suite. In the
    # future, when we have the ability to have custom suites, we should change
    # this to run in a custom suite. You know, in the future.
    beaker_suites.each do |suite|
      @cli.options[suite] = []
    end
    if Pathname(resource).directory?
      @cli.options[:tests] = Dir.glob("#{Pathname(resource).expand_path}/*.rb")
    else
      @cli.options[:tests] = [Pathname(resource).expand_path.to_s]
    end
  elsif resource.match(/pre-suite|tests|post-suite|pre-cleanup/)
    # The regex match here is loose so that users can supply multiple suites,
    # such as `beaker exec pre-suite,tests`.
    beaker_suites.each do |suite|
      @cli.options[suite] = [] unless resource.gsub(/-/, '_').match(suite.to_s)
    end
  else
    raise ArgumentError, "Unable to parse #{resource} with beaker exec"
  end
  @cli.execute!
end

#initObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/beaker/subcommand.rb', line 71

def init()
  if options[:help]
    invoke :help, [], ["init"]
    return
  end

  @cli.parse_options

  # delete unnecessary keys for saving the options
  options_to_write = @cli.configured_options
  # Remove keys we don't want to save
  [:timestamp, :logger, :command_line, :beaker_version, :hosts_file].each do |key|
    options_to_write.delete(key)
  end

  options_to_write = SubcommandUtil.sanitize_options_for_save(options_to_write)

  @cli.logger.notify 'Writing configured options to disk'
  File.open(SubcommandUtil::SUBCOMMAND_OPTIONS, 'w') do |f|
    f.write(options_to_write.to_yaml)
  end
  @cli.logger.notify "Options written to #{SubcommandUtil::SUBCOMMAND_OPTIONS}"

  state = YAML::Store.new(SubcommandUtil::SUBCOMMAND_STATE)
  state.transaction do
    state['provisioned'] = false
  end
end

#provisionObject



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
# File 'lib/beaker/subcommand.rb', line 107

def provision()
  if options[:help]
    invoke :help, [], ["provision"]
    return
  end

  state = YAML::Store.new(SubcommandUtil::SUBCOMMAND_STATE)
  if state.transaction { state['provisioned']}
    SubcommandUtil.error_with('Provisioned SUTs detected. Please destroy and reprovision.')
  end

  @cli.parse_options
  @cli.provision

  # Sanitize the hosts
  cleaned_hosts = SubcommandUtil.sanitize_options_for_save(@cli.combined_instance_and_options_hosts)

  # Update each host provisioned with a flag indicating that it no longer needs
  # provisioning
  cleaned_hosts.each do |host, host_hash|
    host_hash['provision'] = false
  end

  # should we only update the options here with the new host? Or update the settings
  # with whatever new flags may have been provided with provision?
  options_storage = YAML::Store.new(SubcommandUtil::SUBCOMMAND_OPTIONS)
  options_storage.transaction do
    @cli.logger.notify 'updating HOSTS key in subcommand_options'
    options_storage['HOSTS'] = cleaned_hosts
    options_storage['hosts_preserved_yaml_file'] = @cli.options[:hosts_preserved_yaml_file]
  end

  @cli.preserve_hosts_file

  state.transaction do
    state['provisioned'] = true
  end
end