Class: Chef::Knife::WindowsBootstrap

Inherits:
Bootstrap show all
Defined in:
lib/chef/knife/windows_bootstrap.rb

Constant Summary

Constants inherited from Chef::Knife

DEFAULT_SUBCOMMAND_FILES

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args

Instance Method Summary collapse

Methods inherited from Bootstrap

#h, #knife_ssh, #knife_ssh_with_password_auth, #load_template, #render_template, #server_name, #ssh_command, #validate_name_args!

Methods inherited from Chef::Knife

#ask_question, #bulk_delete, category, #configure_chef, #confirm, #create_object, #delete_object, #edit_data, #edit_object, #file_exists_and_is_readable?, #format_for_display, #format_list_for_display, guess_category, inherited, #initialize, list_commands, load_commands, #load_from_file, #msg, msg, #output, #parse_options, #pretty_print, #rest, run, #show_usage, snake_case_name, #stdin, #stdout, subcommand_category, subcommand_class_from, subcommands, subcommands_by_category, unnamed?

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#is_mountedObject



61
62
63
64
65
66
67
68
69
# File 'lib/chef/knife/windows_bootstrap.rb', line 61

def is_mounted
  @net_use ||= Chef::Util::Windows::NetUse.new(@admin_share)
  begin
    @net_use.get_info
    return true
  rescue
    return false
  end
end

#mount_admin_shareObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/knife/windows_bootstrap.rb', line 71

def mount_admin_share
  if @add_mount && !is_mounted
    use = {
      :remote => @admin_share, :local => '',
      :username => config[:user], :password => config[:password]
    }
    @net_use.add(use)
    if is_mounted
      Chef::Log.info("Mounted #{@admin_share} for copying files")
    else
      Chef::Log.fatal("Failed to mount #{@admin_share}")
      exit 1
    end
  end
end

#psexec(args) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/knife/windows_bootstrap.rb', line 94

def psexec(args)
  cmd = ['psexec', @unc_path, "-h", "-w", 'c:\chef\tmp']
  if config[:user]
    cmd << "-u" << config[:user]
  end
  if config[:password]
    cmd << "-p" << config[:password]
  end
  cmd << args
  cmd = cmd.join(' ')
  Chef::Log.debug("system #{cmd}")
  system(cmd)
end

#runObject



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
# File 'lib/chef/knife/windows_bootstrap.rb', line 108

def run
  require 'chef/util/windows/net_use'

  if @name_args.first == nil
    Chef::Log.error("Must pass a node name/ip to windows bootstrap")
    exit 1
  end

  config[:server_name] = @name_args.first
  if Chef::Config[:http_proxy]
    uri = URI.parse(Chef::Config[:http_proxy])
    config[:proxy] = "#{uri.host}:#{uri.port}"
  end

  @unc_path = "\\\\#{config[:server_name]}"
  @admin_share = "#{@unc_path}\\c$"
  path = "#{@admin_share}\\chef"
  etc = "#{path}\\etc"
  tmp = "#{path}\\tmp"

  $stdout.sync = true

  command = render_template(load_template(config[:bootstrap_template]))

  Chef::Log.info("Bootstrapping Chef on #{config[:server_name]}")

  @add_mount = config[:user] != nil && !is_mounted
  mount_admin_share

  begin
    [etc, tmp, "#{path}\\log"].each do |dir|
      unless File.exists?(dir)
        Chef::Log.debug("mkdir_p #{dir}")
        FileUtils.mkdir_p(dir)
      end
    end
    File.open("#{tmp}\\bootstrap.bat", 'w') {|f| f.write(command) }
    FileUtils.cp(File.join(File.dirname(__FILE__), 'bootstrap', 'client-install.vbs'), tmp)
    FileUtils.cp(Chef::Config[:validation_key], etc)
    psexec("cmd.exe /c bootstrap.bat")
  ensure
    unmount_admin_share
  end
end

#unmount_admin_shareObject



87
88
89
90
91
92
# File 'lib/chef/knife/windows_bootstrap.rb', line 87

def unmount_admin_share
  if @add_mount && is_mounted
    Chef::Log.debug("Unmounting #{@admin_share}")
    @net_use.delete
  end
end