lib/chef/knife/topo_import.rb in knife-topo-1.1.2 vs lib/chef/knife/topo_import.rb in knife-topo-2.0.1
- old
+ new
@@ -15,94 +15,80 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/knife'
+require 'chef/knife/cookbook_create'
+require 'chef/knife/topo/command_helper'
+require 'chef/knife/topo/loader'
-require_relative 'topology_helper'
-require_relative 'topo_cookbook_create'
+module KnifeTopo
+ # knife topo import
+ class TopoImport < Chef::Knife
+ deps do
+ require 'chef/knife/topo/processor'
+ end
-class Chef
- class Knife
- class TopoImport < Chef::Knife
+ banner 'knife topo import [ TOPOLOGY_FILE ] (options)'
- deps do
- Chef::Knife::TopoCookbookCreate.load_deps
- end
+ option(
+ :data_bag,
+ short: '-D DATA_BAG',
+ long: '--data-bag DATA_BAG',
+ description: 'The data bag to store the topologies in'
+ )
+ option(
+ :input_format,
+ long: '--input-format FORMAT',
+ description: 'The format to convert from (e.g. topo_v1)'
+ )
- banner "knife topo import [ TOPOLOGY_FILE [ TOPOLOGY ... ]] (options)"
-
- option :data_bag,
- :short => '-D DATA_BAG',
- :long => "--data-bag DATA_BAG",
- :description => "The data bag to store the topologies in"
+ # Make called command options available
+ orig_opts = KnifeTopo::TopoImport.options
+ self.options = Chef::Knife::CookbookCreate.options.merge(orig_opts)
- # Make called command options available
- self.options = Chef::Knife::TopoCookbookCreate.options.merge(self.options)
-
- def initialize (args)
- super
- @topo_cookbook_args = initialize_cmd_args(args, [ 'topo', 'cookbook', '', '' ])
+ include KnifeTopo::CommandHelper
+ include KnifeTopo::Loader
- # All called commands need to accept union of options
- Chef::Knife::TopoCookbookCreate.options = options
- end
+ def initialize(args)
+ super
+ @args = args
+ @topo_file = @name_args[0] || 'topology.json'
+ end
- def run
-
- # load data from the topologies file
- topo_file = @name_args[0] || 'topology.json'
- topologies = load_topologies(topo_file)
- bag_name = topo_bag_name(config[:data_bag])
- topo_names = @name_args[1..-1] if @name_args[1]
-
- # make sure the topology bag directory exists
- path = File.join(topologies_path, bag_name)
- FileUtils.mkdir_p(path)
+ def run
+ @topo = load_topo_from_file_or_exit(@topo_file, config[:input_format])
+ @processor = KnifeTopo::Processor.for_topo(@topo)
+ create_topo_bag_dir
+ import_topo
+ end
- topologies.each do |topo_data|
-
- topo_name = topo_data['name'] || topo_data['id']
- topo_data['id'] ||= topo_name
- topo_data['name'] ||= topo_name
-
- if (!topo_name)
- ui.error "Could not find a topology name - #{topo_file} does not appear to be a valid topology JSON file"
- exit(1)
- end
-
- # check against specific topology list
- if topo_names
- if topo_names.include?(topo_name)
- topo_names.delete(topo_name)
- else
- next
- end
- end
-
- # write the databag for this topology
- path = File.join(topologies_path, bag_name, topo_name + '.json')
- File.open(path,"w") do |f|
- f.write(Chef::JSONCompat.to_json_pretty(topo_data))
- f.close()
- ui.info "Created topology data bag in #{path}"
- end
-
- # run topo cookbook to generate the cookbooks for this topology
- @topo_cookbook_args[2] = topo_name
- @topo_cookbook_args[3] = topo_file
- run_cmd(Chef::Knife::TopoCookbookCreate, @topo_cookbook_args)
- ui.info "Imported topology #{display_name(topo_data)}"
- ui.info("Build information: " + topo_data['buildstamp']) if topo_data['buildstamp']
-
- end
-
- ui.info "Did not find topologies #{topo_names.join(', ')} in the topology json file" if topo_names && topo_names.length > 0
- ui.info "Import finished"
-
+ def create_topo_bag_dir
+ # make sure the topology bag directory exists
+ path = File.join(topologies_path, topo_bag_name)
+ FileUtils.mkdir_p(path)
+ end
+
+ def import_topo
+ write_topo_to_file
+ do_create_artifacts
+
+ ui.info "Imported topology: #{@topo.display_info}"
+ end
+
+ def write_topo_to_file
+ path = File.join(topologies_path, topo_bag_name, @topo['name'] + '.json')
+ File.open(path, 'w') do |f|
+ f.write(Chef::JSONCompat.to_json_pretty(@topo.raw_data))
+ f.close
+ ui.info "Created topology data bag: #{path}"
end
-
- include Chef::Knife::TopologyHelper
+ end
+ def do_create_artifacts
+ @processor.generate_artifacts(
+ 'cmd_args' => @args,
+ 'cmd' => self
+ )
end
end
end