lib/piedesaint/cli.rb in piedesaint-0.2.0 vs lib/piedesaint/cli.rb in piedesaint-0.2.1

- old
+ new

@@ -2,10 +2,13 @@ require 'openssl' require 'yaml' require 'json' +# For automatic IP configuration (modes: *vmware-nat*) +require 'socket' + module Piedesaint # The CLI class encapsulates the behavior of Piedesaint when it is invoked # as a command-line utility. This allows other programs to embed Piedesaint # and preserve its command-line semantics. @@ -13,11 +16,11 @@ def initialize end def execute load_config - cert(@config[:host]) if @config[:refresh_cert] + cert( resolve_host(@config[:host]) ) if @config[:refresh_cert] refresh_asset_provider if @config[:refresh_asset_provider] piedesanto = Piedesaint.new @config piedesanto.start end @@ -50,11 +53,11 @@ def set_host ( host = [] ) load_config @config[:host] = host[0] save_config @config - cert host[0] + cert( resolve_host(@config[:host]) ) end def asset_provider ( parameters = [] ) load_config asset_provider_helper_vagrantfile = parameters[0] || "chef_vagrant" @@ -63,10 +66,22 @@ @config[:asset_provider_config] = asset_provider_config save_config @config end private + def resolve_host ( hostname ) + case hostname + when "*vmware-nat*" + vmnet8_ifs = Socket.getifaddrs.select{ |ifaddr| ifaddr.name == 'vmnet8' && ifaddr.addr.ip? } + return vmnet8_ifs[0].addr.ip_address + else + return hostname + end + + return hostname + end + def load_config config_path = find_default_config_path if config_path.nil? abort "Configuration not provided.\nExecute '#{$PROGRAM_NAME} init' to generate one" end @@ -90,10 +105,11 @@ FileUtils.cd "ssl" do open 'server.key', 'w' do |io| io.write key.to_pem end open 'server.crt', 'w' do |io| io.write cert.to_pem end end end + puts "Created and install certificates for #{cn}" end def refresh_asset_provider return if @config[:asset_provider_helper_vagrantfile].nil? return if @config[:asset_provider_helper_vagrantfile].empty? @@ -101,11 +117,13 @@ return if @config[:asset_provider_config].nil? return if @config[:asset_provider_config].empty? asset_provider_config = JSON.parse(File.read(@config[:asset_provider_config])) - asset_provider_config["asset_provider"]["host"] = @config[:host] + asset_provider_config["asset_provider"]["host"] = resolve_host(@config[:host]) File.write(@config[:asset_provider_config], JSON.pretty_generate(asset_provider_config)) + + puts "Refreshed #{@config[:asset_provider_config]} and #{@config[:asset_provider_helper_vagrantfile]}" end def create_ssl_artifacts ( cn = 'localhost' ) key = OpenSSL::PKey::RSA.new 2048 \ No newline at end of file