knife-block 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,37 +6,63 @@
6
6
  # Copyright (c) Matthew Macdonald-Wallace / Green and Secure IT Limited 2012
7
7
  #
8
8
  module GreenAndSecure
9
+ def check_block_setup
10
+ if File.exists?(::Chef::Knife::chef_config_dir+"/knife.rb") then
11
+ if !File.symlink?(::Chef::Knife::chef_config_dir+"/knife.rb") then
12
+ puts "#{::Chef::Knife::chef_config_dir}/knife.rb is NOT a symlink."
13
+ puts "Please copy the file to #{::Chef::Knife::chef_config_dir}/knife-<servername>.rb and re-run this command."
14
+ exit 3
15
+ end
16
+ end
17
+ end
18
+ module_function :check_block_setup
19
+
20
+ def printable_server(server_config)
21
+ File.basename(server_config, ".rb").split('-')[1 .. -1].join('-')
22
+ end
23
+ module_function :printable_server
24
+
9
25
  class Block < Chef::Knife
10
26
  banner "knife block"
11
27
  def run
12
- puts 'Did you mean to run "knife block list" instead?'
13
- puts 'Running "knife block list" for you now'
14
28
  list = GreenAndSecure::BlockList.new
15
- list.run
29
+ if name_args.size == 1 and list.servers.include?(name_args[0])
30
+ use = GreenAndSecure::BlockUse.new
31
+ use.name_args = name_args
32
+ use.run
33
+ else
34
+ puts 'Did you mean to run "knife block list" instead?'
35
+ puts 'Running "knife block list" for you now'
36
+ list.run
37
+ end
16
38
  end
17
39
  end
40
+
18
41
  class BlockList < Chef::Knife
19
42
 
20
43
  banner "knife block list"
21
44
 
45
+ @current_server = nil
46
+ def current_server
47
+ GreenAndSecure::check_block_setup
48
+
49
+ @current_server ||= if File.exists?(::Chef::Knife::chef_config_dir+"/knife.rb") then
50
+ GreenAndSecure::printable_server(File.readlink(::Chef::Knife::chef_config_dir+"/knife.rb"))
51
+ else
52
+ nil
53
+ end
54
+ end
55
+
56
+ @servers = []
57
+ def servers
58
+ ## get the list of available environments by searching ~/.chef for knife.rb files
59
+ @servers ||= Dir.glob(::Chef::Knife::chef_config_dir+"/knife-*.rb").sort.map do | fn |
60
+ GreenAndSecure::printable_server(fn)
61
+ end
62
+ end
63
+
22
64
  # list the available environments
23
65
  def run
24
- chef_path = Dir.home + "/.chef"
25
- ## get the list of available environments by searching ~/.chef for knife.rb files
26
- file_list = Dir.glob(chef_path+"/knife-*.rb").sort
27
- current_server = ""
28
- if File.exists?(chef_path+"/knife.rb") then
29
- if !File.symlink?(chef_path+"/knife.rb") then
30
- puts "#{chef_path}/knife.rb is NOT a symlink."
31
- puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
32
- exit 3
33
- end
34
- current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
35
- end
36
- servers = []
37
- file_list.each do | fn |
38
- servers << fn.split('-')[1 .. -1].join('-').split('.')[0]
39
- end
40
66
  puts "The available chef servers are:"
41
67
  servers.each do |server|
42
68
  if server == current_server then
@@ -47,6 +73,7 @@ module GreenAndSecure
47
73
  end
48
74
  end
49
75
  end
76
+
50
77
  class BlockUse < Chef::Knife
51
78
 
52
79
  banner "knife block use SERVERNAME"
@@ -58,32 +85,21 @@ module GreenAndSecure
58
85
  server_list.run
59
86
  exit 1
60
87
  end
61
- chef_path = Dir.home + "/.chef"
62
- current_server = ""
63
- if File.exists?(chef_path+"/knife.rb") then
64
- if !File.symlink?(chef_path+"/knife.rb") then
65
- puts "#{chef_path}/knife.rb is NOT a symlink."
66
- puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
67
- exit 3
68
- end
69
- current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
70
- end
88
+ list = GreenAndSecure::BlockList.new
71
89
  new_server = name_args.first
72
- ui.confirm("You are asking to change from #{current_server} to #{new_server}. Are you sure")
73
- if File.exists?(chef_path+"/knife.rb")
74
- File.unlink(chef_path+"/knife.rb")
90
+ ui.confirm("You are asking to change from #{list.current_server} to #{new_server}. Are you sure")
91
+ if File.exists?(::Chef::Knife::chef_config_dir+"/knife.rb")
92
+ File.unlink(::Chef::Knife::chef_config_dir+"/knife.rb")
75
93
  end
76
- File.symlink(chef_path+"/knife-#{new_server}.rb",chef_path+"/knife.rb")
77
- if File.exists?(chef_path+"/knife.rb") then
78
- current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
79
- end
80
- puts "The knife configuration has been updated to use #{current_server}"
94
+ File.symlink(::Chef::Knife::chef_config_dir+"/knife-#{new_server}.rb",
95
+ ::Chef::Knife::chef_config_dir+"/knife.rb")
96
+ puts "The knife configuration has been updated to use #{new_server}"
81
97
  end
82
98
  end
99
+
83
100
  class BlockNew < Chef::Knife
84
101
  banner "knife block new SERVERNAME"
85
102
  def run
86
- chef_path = Dir.home + "/.chef"
87
103
  puts "This will create a new knife configuration file for you to use with knife-block"
88
104
  unless name_args.size == 1
89
105
  @config_name = ui.ask_question("Please provide a friendly name for the new configuration file: ")
@@ -94,28 +110,16 @@ module GreenAndSecure
94
110
  @chef_server = ui.ask_question("Please enter the url to your Chef Server: ")
95
111
  require 'ohai'
96
112
  require 'chef/knife/configure'
97
- if File.exists?(chef_path+"/knife.rb") then
98
- if !File.symlink?(chef_path+"/knife.rb") then
99
- puts "#{chef_path}/knife.rb already exists!"
100
- puts "Please copy the file to #{chef_path}/knife-<servername>.rb and re-run this command."
101
- exit 3
102
- end
103
- end
113
+ GreenAndSecure::check_block_setup
104
114
  knife_config = Chef::Knife::Configure.new
105
- knife_config.config[:config_file] = "#{chef_path}/knife-#{@config_name}.rb"
115
+ knife_config.config[:config_file] = "#{::Chef::Knife::chef_config_dir}/knife-#{@config_name}.rb"
106
116
  knife_config.config[:chef_server_url] = @chef_server
107
117
  knife_config.run
108
- puts "#{chef_path}/knife-#{@config_name}.rb has been sucessfully created"
118
+ puts "#{::Chef::Knife::chef_config_dir}/knife-#{@config_name}.rb has been sucessfully created"
109
119
  GreenAndSecure::BlockList.new.run
110
- ui.confirm("Would you like to switch to it now? ")
111
- if File.exists?(chef_path+"/knife.rb")
112
- File.unlink(chef_path+"/knife.rb")
113
- end
114
- File.symlink(chef_path+"/knife-#{@config_name}.rb",chef_path+"/knife.rb")
115
- if File.exists?(chef_path+"/knife.rb") then
116
- current_server = File.readlink(chef_path+"/knife.rb").split('-')[1 .. -1].join('-').split('.')[0]
117
- end
118
- puts "The knife configuration has been updated to use #{current_server}"
120
+ use = GreenAndSecure::BlockUse.new
121
+ use.name_args = [ @config_name ]
122
+ use.run
119
123
  end
120
124
  end
121
125
  end
@@ -1,5 +1,5 @@
1
1
  module Knife
2
2
  module Block
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-block
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-06 00:00:00.000000000Z
12
+ date: 2012-04-18 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Create and manage knife.rb files for OpsCodes' Chef
15
15
  email: