esage-chef-agent 2.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/esage-chef-run +208 -0
  2. metadata +158 -0
@@ -0,0 +1,208 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright (C) 2008 Esage Holdings S.L.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'rubygems'
18
+ require 'fileutils'
19
+ require 'rest-client'
20
+ require 'xmlsimple'
21
+ require 'esage-chef-agent'
22
+ require 'logger'
23
+ include Esage::Chef
24
+
25
+ at_exit do
26
+ if $!
27
+ log "BACKTRACE", :error
28
+ log $!.to_s, :error
29
+ log $@.to_s, :error
30
+ end
31
+ end
32
+
33
+ Log = Logger.new Esage::Chef::Config.log_file
34
+
35
+ def log(msg, level = :info)
36
+ if level == :debug and not $DEBUG
37
+ return
38
+ end
39
+ if level == :info
40
+ Log.info msg.to_s
41
+ elsif level == :warning
42
+ Log.warn msg.to_s
43
+ elsif level == :error
44
+ Log.error msg.to_s
45
+ else
46
+ Log.debug msg.to_s
47
+ end
48
+ end
49
+
50
+ CHEF_CONFIG_DIR = Esage::Chef::Config.chef_config_dir
51
+ BOOTSTRAP_XML = Esage::Chef::Config.bootstrap_backup_file
52
+ CLIENT_CERT = Esage::Chef::Config.client_cert_file
53
+
54
+ if File.exist? CLIENT_CERT
55
+ log "#{CLIENT_CERT} file found. If you want to re-run the Esage Chef Agent,\n" +
56
+ "delete the #{Esage::Chef::Config.chef_config_dir} directory first.\n" +
57
+ "Aborting.", :warning
58
+ exit 1
59
+ end
60
+
61
+ if not File.directory? CHEF_CONFIG_DIR
62
+ log "Creating #{CHEF_CONFIG_DIR} directory."
63
+ FileUtils.mkdir_p '/etc/chef'
64
+ end
65
+
66
+ #
67
+ # Try to fix system clock with ntpdate
68
+ #
69
+ log "Trying to synchronize system clock"
70
+ `/usr/sbin/ntpdate #{Esage::Chef::Config.ntp_server} > /dev/null 2>&1`
71
+ if $? != 0
72
+ log "Could not update the system clock using ntpdate and #{Esage::Chef::Config.ntp_server} ntp server", :warning
73
+ end
74
+
75
+ #
76
+ # Parse info from DCHP client leases file
77
+ #
78
+ log "Parsing leases file"
79
+ leases = Util.parse_leases_file
80
+ if not leases
81
+ log "Leases file not found or invalid. Current leases search path ['/var/lib/dhcp3', '/var/lib/dhcp', '/var/lib/dhclient', '/var/lib/NetworkManager'].", :error
82
+ exit 1
83
+ else
84
+ log "Leases found #{leases.inspect}"
85
+
86
+ #
87
+ # Request node info from Esage API
88
+ #
89
+ log "Requesting Chef config from API #{leases[:esage_api_url]} OneTime #{leases[:esage_api_token]} Accept #{Esage::Chef::Config.bootstrap_mediatype}"
90
+ begin
91
+ xml = RestClient::Resource.new(
92
+ leases[:esage_api_url],
93
+ :verify_ssl => OpenSSL::SSL::VERIFY_NONE
94
+ ).get(
95
+ :authorization => "OneTime #{leases[:esage_api_token]}",
96
+ :accept => Esage::Chef::Config.bootstrap_mediatype
97
+ )
98
+ rescue Exception => e
99
+ log "Error requesting node info from API", :error
100
+ log e.message, :error
101
+ log e.backtrace.join("\n ")
102
+ exit 1
103
+ end
104
+ #
105
+ # Write the bootstrap XML
106
+ # Daemon will not run if this XML is found
107
+ #
108
+ File.open(BOOTSTRAP_XML, 'w') do |f|
109
+ f.puts xml
110
+ end
111
+
112
+ #
113
+ # Parse the XML returned by API
114
+ #
115
+ log "Parsing Bootstrap XML from API"
116
+ begin
117
+ bootstrap_config = BootstrapConfigParser.new(xml)
118
+ rescue Exception => e
119
+ log "Error parsing XML bootstrap file", :error
120
+ log e.message, :error
121
+ log e.backtrace.join("\n ")
122
+ exit 1
123
+ end
124
+
125
+ #
126
+ # Write Chef validation pem
127
+ #
128
+ File.open(Esage::Chef::Config.validation_cert, 'w') do |f|
129
+ f.puts bootstrap_config.validation_cert
130
+ end
131
+ log "Validation cert written"
132
+
133
+ #
134
+ # Get required node info and write chef-client config file
135
+ #
136
+ validation_client_name = bootstrap_config.validation_client_name
137
+ chef_server_url = bootstrap_config.chef_server_url
138
+ node_config = bootstrap_config.node_config
139
+ if node_config['run_list']
140
+ log "Recipes found #{node_config.inspect}"
141
+ else
142
+ log "No recipes selected", :warning
143
+ end
144
+ File.open('/etc/chef/client.rb', 'w') do |f|
145
+ f.puts "log_level :info"
146
+ f.puts "log_location STDOUT"
147
+ f.puts "chef_server_url '#{chef_server_url}'"
148
+ f.puts "ssl_verify_mode :verify_none"
149
+ f.puts "validation_client_name '#{validation_client_name}'"
150
+ end
151
+
152
+ #
153
+ # Write first-boot.json attributes
154
+ #
155
+ File.open('/etc/chef/first-boot.json', 'w') do |f|
156
+ node_config ||= {}
157
+ f.puts node_config.to_json
158
+ end
159
+ log "Written '#{node_config.to_json}' to /etc/chef/first-boot.json file"
160
+
161
+
162
+ #
163
+ # Set the hostname
164
+ #
165
+ node_name = bootstrap_config.node_name
166
+
167
+ log "Setting hostname"
168
+ output = `hostname #{node_name} 2>&1`
169
+ log output, :info
170
+
171
+ net_config_file = '/etc/sysconfig/network'
172
+ if File.exists? net_config_file
173
+ log "Configuring /etc/sysconfig/network with the new hostname"
174
+ data = File.read net_config_file
175
+ File.open net_config_file, 'w' do |f|
176
+ data.each_line {|l| l.include?('HOSTNAME') ? f.puts("HOSTNAME=#{node_name}\n") : f.puts(l)}
177
+ end
178
+ end
179
+ File.open('/etc/hostname', 'w') do |f|
180
+ f.puts node_name
181
+ end
182
+ File.open('/etc/hosts', 'a') do |f|
183
+ f.puts "127.0.0.1 #{node_name}"
184
+ end
185
+ log "done"
186
+
187
+ #
188
+ # Everything in place, so run the client
189
+ cmd = "chef-client -N #{node_name} --once -j /etc/chef/first-boot.json -L /var/log/chef-client.log"
190
+ log "Running chef-client first time"
191
+ log cmd
192
+ output = `#{cmd}`
193
+ #
194
+ # Remove validation certs and bootstrap XML
195
+ #
196
+ if not ENV['ESAGE_DEBUG']
197
+ if File.exist?(Esage::Chef::Config.validation_cert)
198
+ FileUtils.rm(Esage::Chef::Config.validation_cert)
199
+ end
200
+ if File.exist?(Esage::Chef::Config.bootstrap_backup_file)
201
+ FileUtils.rm(Esage::Chef::Config.bootstrap_backup_file)
202
+ end
203
+ end
204
+ if $? != 0
205
+ log "chef-client run failed", :error
206
+ log output, :info
207
+ end
208
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: esage-chef-agent
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 2
8
+ - 0
9
+ - 11
10
+ version: 2.0.11
11
+ platform: ruby
12
+ authors:
13
+ - Sam Yang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2016-04-22 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ hash: 15
29
+ segments:
30
+ - 3
31
+ - 2
32
+ - 0
33
+ version: 3.2.0
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec-collection_matchers
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - "="
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 1
48
+ - 2
49
+ version: 1.1.2
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: simplecov
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - "="
59
+ - !ruby/object:Gem::Version
60
+ hash: 57
61
+ segments:
62
+ - 0
63
+ - 9
64
+ - 1
65
+ version: 0.9.1
66
+ type: :development
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: chef
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ type: :runtime
81
+ version_requirements: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ name: rest-client
84
+ prerelease: false
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - "="
89
+ - !ruby/object:Gem::Version
90
+ hash: 55
91
+ segments:
92
+ - 1
93
+ - 8
94
+ - 0
95
+ version: 1.8.0
96
+ type: :runtime
97
+ version_requirements: *id005
98
+ - !ruby/object:Gem::Dependency
99
+ name: xml-simple
100
+ prerelease: false
101
+ requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - "="
105
+ - !ruby/object:Gem::Version
106
+ hash: 25
107
+ segments:
108
+ - 1
109
+ - 1
110
+ - 5
111
+ version: 1.1.5
112
+ type: :runtime
113
+ version_requirements: *id006
114
+ description: Esage Chef Agent
115
+ email: support@esage.cn
116
+ executables:
117
+ - esage-chef-run
118
+ extensions: []
119
+
120
+ extra_rdoc_files: []
121
+
122
+ files:
123
+ - bin/esage-chef-run
124
+ homepage: http://github.com/esage-cn/esage-chef-agent
125
+ licenses:
126
+ - Apache License 2.0
127
+ post_install_message:
128
+ rdoc_options: []
129
+
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ hash: 3
138
+ segments:
139
+ - 0
140
+ version: "0"
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ hash: 3
147
+ segments:
148
+ - 0
149
+ version: "0"
150
+ requirements: []
151
+
152
+ rubyforge_project:
153
+ rubygems_version: 1.8.15
154
+ signing_key:
155
+ specification_version: 3
156
+ summary: Esage Chef Agent
157
+ test_files: []
158
+