Class: KnifeSharp::SharpServer

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/sharp-server.rb

Instance Method Summary collapse

Instance Method Details

#current_serverObject



72
73
74
75
# File 'lib/chef/knife/sharp-server.rb', line 72

def current_server
  cur_conf = knife_conf
  extract_server(File.readlink(cur_conf)) if File.exists?(cur_conf) and File.symlink?(cur_conf)
end

#extract_server(config_filename) ⇒ Object



62
63
64
65
# File 'lib/chef/knife/sharp-server.rb', line 62

def extract_server(config_filename)
  match = config_filename.match(/#{Chef::Knife::chef_config_dir}\/knife-(\w+)\.rb/)
  match ? match[1] : nil
end

#knife_conf(server = nil) ⇒ Object



67
68
69
70
# File 'lib/chef/knife/sharp-server.rb', line 67

def knife_conf(server = nil)
  srv = server ? "-#{server}" : ""
  File.join(Chef::Knife::chef_config_dir, "knife#{srv}.rb")
end

#list_configsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef/knife/sharp-server.rb', line 39

def list_configs
  if config[:machine] == true
    server = current_server()
    if server
      ui.msg server
    else
      ui.msg "invalid"
    end
  else
    avail_confs = Dir.glob(File.join(Chef::Knife::chef_config_dir, "knife-*.rb"))
    if !avail_confs.empty?
      ui.msg "Available servers:"
      avail_confs.each do |file|
        server = extract_server(file)
        prefix = (server == current_server) ? ">> " : "   "
        ui.msg "#{prefix}#{server} (#{file})"
      end
    else
      ui.msg "No knife server configuration file found."
    end
  end
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/chef/knife/sharp-server.rb', line 12

def run
  if File.exists?(knife_conf) and !File.symlink?(knife_conf)
    ui.error "#{knife_conf} is not a symlink."
    ui.msg "Copy it to #{knife_conf("<server name>")} and try again."
    exit 1
  end
  unless name_args.size == 1
    list_configs
    exit 0
  end
  update_config(name_args.first)
end

#update_config(new_server) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chef/knife/sharp-server.rb', line 25

def update_config(new_server)
  cur_conf = knife_conf
  new_conf = knife_conf(new_server)
  if File.exists?(new_conf)
    File.unlink(cur_conf) if File.exists?(cur_conf)
    File.symlink(new_conf, cur_conf)
    ui.msg "The knife configuration has been updated to use #{new_server}."
  else
    ui.error "Knife configuration for #{new_server} not found."
    list_configs
    exit 1
  end
end