knife-setup 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitigonre ADDED
@@ -0,0 +1 @@
1
+ pkg
data/README.md ADDED
@@ -0,0 +1,6 @@
1
+ ```
2
+ gem install knife-setup
3
+ ```
4
+ ```ruby
5
+ knife setup --help
6
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'knife-setup/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "knife-setup"
7
+ s.version = Knife::Setup::VERSION
8
+ s.authors = ["Vasiliev D.V."]
9
+ s.email = %w(vadv.mkn@gmail.com)
10
+ s.homepage = "https://github.com/vadv/knife-setup"
11
+ s.summary = %q{Setup chef on machine.}
12
+ s.description = %q{Allows you to bootsrap machine and set role and env.}
13
+ s.licenses = %w(MIT)
14
+
15
+ s.add_dependency('chef')
16
+
17
+ s.rubyforge_project = "knife-setup"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = %w(lib)
23
+ end
@@ -0,0 +1,235 @@
1
+ require 'chef/knife'
2
+ require 'erubis'
3
+
4
+ class Chef
5
+ class Knife
6
+ class Setup < Knife
7
+
8
+ deps do
9
+ require 'chef/knife/core/bootstrap_context'
10
+ require 'chef/json_compat'
11
+ require 'tempfile'
12
+ require 'highline'
13
+ require 'net/ssh'
14
+ require 'net/ssh/multi'
15
+ Chef::Knife::Ssh.load_deps
16
+ end
17
+
18
+ banner "knife setup FQDN (options)"
19
+
20
+ option :ssh_user,
21
+ :short => "-x USERNAME",
22
+ :long => "--ssh-user USERNAME",
23
+ :description => "The ssh username",
24
+ :default => "root"
25
+
26
+ option :ssh_password,
27
+ :short => "-P PASSWORD",
28
+ :long => "--ssh-password PASSWORD",
29
+ :description => "The ssh password"
30
+
31
+ option :ssh_port,
32
+ :short => "-p PORT",
33
+ :long => "--ssh-port PORT",
34
+ :description => "The ssh port",
35
+ :default => "22",
36
+ :proc => Proc.new { |key| Chef::Config[:knife][:ssh_port] = key }
37
+
38
+ option :identity_file,
39
+ :short => "-i IDENTITY_FILE",
40
+ :long => "--identity-file IDENTITY_FILE",
41
+ :description => "The SSH identity file used for authentication"
42
+
43
+ option :chef_node_name,
44
+ :short => "-N NAME",
45
+ :long => "--node-name NAME",
46
+ :description => "The Chef node name for your new node"
47
+
48
+ option :prerelease,
49
+ :long => "--prerelease",
50
+ :description => "Install the pre-release chef gems"
51
+
52
+ option :bootstrap_version,
53
+ :long => "--bootstrap-version VERSION",
54
+ :description => "The version of Chef to install",
55
+ :proc => lambda { |v| Chef::Config[:knife][:bootstrap_version] = v }
56
+
57
+ option :bootstrap_proxy,
58
+ :long => "--bootstrap-proxy PROXY_URL",
59
+ :description => "The proxy server for the node being bootstrapped",
60
+ :proc => Proc.new { |p| Chef::Config[:knife][:bootstrap_proxy] = p }
61
+
62
+ option :distro,
63
+ :short => "-d DISTRO",
64
+ :long => "--distro DISTRO",
65
+ :description => "Bootstrap a distro using a template",
66
+ :default => "debian6"
67
+
68
+ option :use_sudo,
69
+ :long => "--sudo",
70
+ :description => "Execute the bootstrap via sudo",
71
+ :boolean => true
72
+
73
+ option :template_file,
74
+ :long => "--template-file TEMPLATE",
75
+ :description => "Full path to location of template to use",
76
+ :default => false
77
+
78
+ option :environment,
79
+ :long => "--environment ENVIRONMENT",
80
+ :description => "Set environment for node",
81
+ :default => "_default"
82
+
83
+ option :runchef,
84
+ :long => "--runchef",
85
+ :description => "Run chef after install bootstrap",
86
+ :boolean => true,
87
+ :default => false
88
+
89
+ option :nobootstrap,
90
+ :long => "--nobootstrap",
91
+ :description => "Not run bootstrap scripts",
92
+ :boolean => false,
93
+ :default => false
94
+
95
+ option :run_list,
96
+ :short => "-r RUN_LIST",
97
+ :long => "--run-list RUN_LIST",
98
+ :description => "Comma separated list of roles/recipes to apply",
99
+ :proc => lambda { |o| o.split(/[\s,]+/) },
100
+ :default => []
101
+
102
+ option :no_host_key_verify,
103
+ :long => "--no-host-key-verify",
104
+ :description => "Disable host key verification",
105
+ :boolean => true,
106
+ :default => false
107
+
108
+ def load_template(template=nil)
109
+ # Are we bootstrapping using an already shipped template?
110
+ if config[:template_file]
111
+ bootstrap_files = config[:template_file]
112
+ else
113
+ bootstrap_files = []
114
+ bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{config[:distro]}.erb")
115
+ bootstrap_files << File.join(@@chef_config_dir, "bootstrap", "#{config[:distro]}.erb")
116
+ bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb")
117
+ bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{config[:distro]}.erb"))
118
+ bootstrap_files.flatten!
119
+ end
120
+
121
+ template = Array(bootstrap_files).find do |bootstrap_template|
122
+ Chef::Log.debug("Looking for bootstrap template in #{File.dirname(bootstrap_template)}")
123
+ File.exists?(bootstrap_template)
124
+ end
125
+
126
+ unless template
127
+ ui.info("Can not find bootstrap definition for #{config[:distro]}")
128
+ raise Errno::ENOENT
129
+ end
130
+
131
+ Chef::Log.debug("Found bootstrap template in #{File.dirname(template)}")
132
+
133
+ IO.read(template).chomp
134
+ end
135
+
136
+ def render_template(template=nil)
137
+ context = Knife::Core::BootstrapContext.new(config, config[:run_list], Chef::Config)
138
+ Erubis::Eruby.new(template).evaluate(context)
139
+ end
140
+
141
+ def get_node
142
+ node = Chef::Node.load(@node_name)
143
+ if node.nil?
144
+ ui.error("Could not find a node named #{@node_name}")
145
+ exit 1
146
+ end
147
+ return node
148
+ end
149
+
150
+ def set_run_list(node, run_list)
151
+ ui.info("Setting run_list to #{run_list.inspect} for #{node.name}")
152
+ node.run_list.reset!(run_list)
153
+ node.save
154
+ end
155
+
156
+ def set_env(node, env)
157
+ ui.info("Setting environment to #{env} for #{node.name}")
158
+ node.chef_environment(env)
159
+ node.save
160
+ end
161
+
162
+ def bootstrap_client
163
+ begin
164
+ knife_ssh(ssh_command).run
165
+ rescue Net::SSH::AuthenticationFailed
166
+ unless config[:ssh_password]
167
+ puts "Failed to authenticate #{config[:ssh_user]} - trying password auth"
168
+ knife_ssh_with_password_auth(ssh_command).run
169
+ end
170
+ end
171
+ end
172
+
173
+ def run
174
+
175
+ validate_name_args!
176
+ @node_name = Array(@name_args).first
177
+ # back compat--templates may use this setting:
178
+ config[:server_name] = @node_name
179
+
180
+ $stdout.sync = true
181
+
182
+ ui.info("Setup Chef on #{ui.color(@node_name, :bold)}")
183
+
184
+ bootstrap_client unless config[:nobootstrap]
185
+ set_run_list(get_node, config[:run_list]) if config[:run_list]
186
+ set_env(get_node, config[:environment]) unless config[:environment] == "_default"
187
+ knife_ssh("chef-client").run if config[:runchef]
188
+
189
+ end
190
+
191
+ def validate_name_args!
192
+ if Array(@name_args).first.nil?
193
+ ui.error("Must pass an FQDN or ip to bootstrap")
194
+ exit 1
195
+ end
196
+ end
197
+
198
+ def server_name
199
+ Array(@name_args).first
200
+ end
201
+
202
+ def knife_ssh(command)
203
+ ssh = Chef::Knife::Ssh.new
204
+ ssh.ui = ui
205
+ ssh.name_args = [ server_name, command]
206
+ ssh.config[:ssh_user] = config[:ssh_user]
207
+ ssh.config[:ssh_password] = config[:ssh_password]
208
+ ssh.config[:ssh_port] = Chef::Config[:knife][:ssh_port] || config[:ssh_port]
209
+ ssh.config[:identity_file] = config[:identity_file]
210
+ ssh.config[:manual] = true
211
+ ssh.config[:no_host_key_verify] = config[:no_host_key_verify]
212
+ ssh.config[:on_error] = :raise
213
+ ssh
214
+ end
215
+
216
+ def knife_ssh_with_password_auth(command)
217
+ ssh = knife_ssh(command)
218
+ ssh.config[:identity_file] = nil
219
+ ssh.config[:ssh_password] = ssh.get_password
220
+ ssh
221
+ end
222
+
223
+ def ssh_command
224
+ command = render_template(load_template(config[:bootstrap_template]))
225
+
226
+ if config[:use_sudo]
227
+ command = "sudo #{command}"
228
+ end
229
+
230
+ command
231
+ end
232
+
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,5 @@
1
+ module Knife
2
+ module Setup
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-setup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vasiliev D.V.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Allows you to bootsrap machine and set role and env.
31
+ email:
32
+ - vadv.mkn@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitigonre
38
+ - README.md
39
+ - Rakefile
40
+ - knife-setup.gemspec
41
+ - lib/chef/knife/knife-setup.rb
42
+ - lib/knife-setup/version.rb
43
+ homepage: https://github.com/vadv/knife-setup
44
+ licenses:
45
+ - MIT
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project: knife-setup
64
+ rubygems_version: 1.8.24
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Setup chef on machine.
68
+ test_files: []
69
+ has_rdoc: