factor-connector-chef 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/factor/connector/chef.rb +102 -22
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51ef48d37d57d75484ac216ea3269273cd2bb374
4
- data.tar.gz: 1912f728b59f8082716284c3bf2bfc3d85ca46b9
3
+ metadata.gz: 374cfde72a6fbc2f4b46b50e1556979a2f67b335
4
+ data.tar.gz: 94464ba0a1406322f018513abf4abf017fe5b155
5
5
  SHA512:
6
- metadata.gz: 5417de7c5266663e0f83113f0a23609508307bf95b91cb9beb75f0aff7c88f706995aeea6e4947adfa5274b6ebf7dda575537ebdf14d66109f2764dcc6d2cb56
7
- data.tar.gz: 5129a89ef81579e18c76eca7a5e02a805ffa69dd94eeb5329d6b5dda481d4bd91fe83f8e5380908162be35b495dae0cbbfa04c5fd82b0689adace546e8f8996d
6
+ metadata.gz: c50402b3b55474ba5b6ff06b64c1668d1a49c9ca8de19b28ff1af345027a0e42946b2c0b02e2321a57a8c4bcb3488948ea6b8e6005b55ee7b24869be4dc21cb1
7
+ data.tar.gz: b36543bfc3f3c079df3ac094ac8616c7e21272cd0d6531ec4f95127b40c00d3fd937680457979567e5014dd10d9fc2d991eb973bea20452876c081f4cd03e833
@@ -4,6 +4,50 @@ require 'net/scp'
4
4
  require 'tempfile'
5
5
  require 'uri'
6
6
 
7
+ class Net::SSH::Connection::Session
8
+ class CommandFailed < StandardError
9
+ end
10
+
11
+ class CommandExecutionFailed < StandardError
12
+ end
13
+
14
+ def exec_sc!(command)
15
+ stdout_data,stderr_data = "",""
16
+ exit_code,exit_signal = nil,nil
17
+ self.open_channel do |channel|
18
+ channel.exec(command) do |_, success|
19
+ raise CommandExecutionFailed, "Command \"#{command}\" was unable to execute" unless success
20
+
21
+ channel.on_data do |_,data|
22
+ stdout_data += data
23
+ end
24
+
25
+ channel.on_extended_data do |_,_,data|
26
+ stderr_data += data
27
+ end
28
+
29
+ channel.on_request("exit-status") do |_,data|
30
+ exit_code = data.read_long
31
+ end
32
+
33
+ channel.on_request("exit-signal") do |_, data|
34
+ exit_signal = data.read_long
35
+ end
36
+ end
37
+ end
38
+ self.loop
39
+
40
+ raise CommandFailed, "Command \"#{command}\" returned exit code #{exit_code}" unless exit_code == 0
41
+
42
+ {
43
+ stdout:stdout_data,
44
+ stderr:stderr_data,
45
+ exit_code:exit_code,
46
+ exit_signal:exit_signal
47
+ }
48
+ end
49
+ end
50
+
7
51
  Factor::Connector.service 'chef' do
8
52
  action 'bootstrap' do |params|
9
53
  host_param = params['host']
@@ -12,26 +56,30 @@ Factor::Connector.service 'chef' do
12
56
  runlist = params['runlist']
13
57
  organization = params['organization']
14
58
  node_name = params['name']
59
+ environment = params['environment']
60
+ chef_server = params['chef_server']
61
+ output = {}
15
62
 
16
63
  fail 'Host is required' unless host_param
17
64
  fail 'Private Key (private_key) is required' unless private_key
18
65
  fail 'Validation Key (validation_key) is required' unless validation_key
19
- fail 'Organization (organization) is required' unless organization
66
+ fail 'Organization (organization) or Chef Server URL (chef_server) is required' unless organization || chef_server
67
+ fail 'Organization (organization) or Chef Server URL (chef_server) is required, but not both' if organization && chef_server
20
68
  fail 'Runlist (runlist) is required' unless runlist
21
69
  fail 'Node Name (name) is required' unless node_name
22
70
 
23
71
  validation_name = params['validation_name'] || "#{organization}-validator"
24
72
 
73
+ chef_server ||= "https://api.opscode.com/organizations/#{organization}"
74
+ info "Using '#{chef_server}' as the Chef Server address"
25
75
 
26
76
  info 'Setting up the client.rb file'
27
77
  client_rb = ""
28
78
  client_rb << "log_location STDOUT\n"
29
- client_rb << "chef_server_url \"https://api.opscode.com/organizations/#{organization}\"\n"
79
+ client_rb << "chef_server_url \"#{chef_server}\"\n"
30
80
  client_rb << "validation_client_name \"#{validation_name}\"\n"
31
81
  client_rb << "node_name \"#{node_name}\"\n"
32
82
 
33
-
34
-
35
83
  info 'Setting up private key'
36
84
  begin
37
85
  private_key_file = Tempfile.new('private')
@@ -56,30 +104,52 @@ Factor::Connector.service 'chef' do
56
104
  fail 'User (user) is required in host address' unless user
57
105
  fail 'Host variable must specific host address' unless host
58
106
 
107
+ install_command = 'curl -L https://www.opscode.com/chef/install.sh | sudo bash'
59
108
  setup_commands = [
60
- 'curl -L https://www.opscode.com/chef/install.sh | sudo bash',
109
+ install_command,
61
110
  'mkdir -p /etc/chef',
62
111
  'cd /etc/chef',
63
112
  ]
64
113
 
114
+ chef_client_options = {}
115
+ chef_client_options['runlist'] = runlist
116
+ chef_client_options['environment'] = environment if environment
117
+
118
+ run_command = []
119
+ chef_client_options.each do |option, value|
120
+ run_command << "--#{option} #{value}"
121
+ end
122
+
65
123
  run_commands = [
66
- "chef-client --runlist #{runlist}"
124
+ "chef-client #{run_command.join(' ')}"
67
125
  ]
68
126
 
69
- output = []
70
127
  begin
71
128
  Net::SSH.start(host, user, ssh_settings) do |ssh|
72
129
 
73
130
  info 'Running setup commands'
74
131
  setup_commands.each do |command|
75
132
  info " running '#{command}'"
76
- returned = ssh.exec!(command)
77
- if returned && returned.is_a?(String)
78
- lines = returned.split("\n")
79
- lines.each do |line|
80
- info " #{line}"
133
+ output[command] ||= []
134
+ returned = ssh.exec_sc!(command)
135
+
136
+ returned[:stdout].split("\n").each do |line|
137
+ if returned[:exit_code]==0
138
+ output[command] << line
139
+ else
140
+ error " #{line}"
141
+ end
142
+ end
143
+ returned[:stderr].split("\n").each do |line|
144
+ output[command] << line
145
+ end
146
+
147
+ if command == install_command
148
+ if returned[:stdout].include?('Thank you for installing Chef!') && returned[:exit_code]==0
149
+ info "Chef installed successfully"
150
+ else
151
+ fail "Install failed"
81
152
  end
82
- output = output + lines
83
153
  end
84
154
  end
85
155
 
@@ -97,14 +167,26 @@ Factor::Connector.service 'chef' do
97
167
 
98
168
  info 'Running chef bootstrap commands'
99
169
  run_commands.each do |command|
170
+ output[command] ||=[]
100
171
  info " running '#{command}'"
101
- returned = ssh.exec!(command)
102
- if returned && returned.is_a?(String)
103
- lines = returned.split("\n")
104
- lines.each do |line|
105
- info " #{line}"
172
+
173
+ returned = ssh.exec_sc!(command)
174
+
175
+ returned[:stdout].split("\n").each do |line|
176
+ if returned[:exit_code]==0
177
+ output[command] << line
178
+ else
179
+ error " #{line}"
106
180
  end
107
- output = output + lines
181
+ end
182
+ returned[:stderr].split("\n").each do |line|
183
+ output[command] << line
184
+ end
185
+
186
+ if returned[:exit_code]==0
187
+ info "Command '#{command}' finished successfully"
188
+ else
189
+ fail "Command '#{command}' failed to run, exit code: #{returned[:exit_code]}"
108
190
  end
109
191
  end
110
192
  end
@@ -119,9 +201,7 @@ Factor::Connector.service 'chef' do
119
201
  private_key_file.unlink
120
202
  validation_key_file.unlink
121
203
  rescue
122
- warn 'Failed to clean up, but no worries, work will go on.'
123
204
  end
124
-
125
- action_callback output: output
205
+ action_callback status: 'complete', output: output
126
206
  end
127
207
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factor-connector-chef
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Skierkowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-13 00:00:00.000000000 Z
11
+ date: 2014-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh