right_link 5.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. data/actors/agent_manager.rb +88 -0
  2. data/actors/instance_scheduler.rb +321 -0
  3. data/actors/instance_services.rb +64 -0
  4. data/actors/instance_setup.rb +567 -0
  5. data/bin/cloud +25 -0
  6. data/bin/cook_runner +44 -0
  7. data/bin/deploy +120 -0
  8. data/bin/enroll +385 -0
  9. data/bin/rad +32 -0
  10. data/bin/rchk +29 -0
  11. data/bin/rnac +39 -0
  12. data/bin/rs_connect +33 -0
  13. data/bin/rs_log_level +31 -0
  14. data/bin/rs_ohai +28 -0
  15. data/bin/rs_reenroll +31 -0
  16. data/bin/rs_run_recipe +34 -0
  17. data/bin/rs_run_right_script +34 -0
  18. data/bin/rs_shutdown +33 -0
  19. data/bin/rs_tag +33 -0
  20. data/bin/rs_thunk +33 -0
  21. data/bin/rstat +31 -0
  22. data/bin/system +16 -0
  23. data/ext/Rakefile +18 -0
  24. data/init/config.yml +5 -0
  25. data/init/init.rb +79 -0
  26. data/lib/chef/ohai_setup.rb +51 -0
  27. data/lib/chef/plugins/cloud.rb +91 -0
  28. data/lib/chef/plugins/cloudstack.rb +23 -0
  29. data/lib/chef/plugins/ec2.rb +23 -0
  30. data/lib/chef/plugins/linux/block_device2.rb +24 -0
  31. data/lib/chef/plugins/rackspace.rb +23 -0
  32. data/lib/chef/plugins/rightscale.rb +125 -0
  33. data/lib/chef/plugins/windows/network.rb +114 -0
  34. data/lib/chef/plugins.rb +74 -0
  35. data/lib/chef/providers/dns_dnsmadeeasy_provider.rb +81 -0
  36. data/lib/chef/providers/dns_resource.rb +100 -0
  37. data/lib/chef/providers/executable_schedule_provider.rb +70 -0
  38. data/lib/chef/providers/executable_schedule_resource.rb +144 -0
  39. data/lib/chef/providers/remote_recipe_provider.rb +86 -0
  40. data/lib/chef/providers/remote_recipe_resource.rb +101 -0
  41. data/lib/chef/providers/right_link_tag_provider.rb +73 -0
  42. data/lib/chef/providers/right_link_tag_resource.rb +59 -0
  43. data/lib/chef/providers/right_script_provider.rb +190 -0
  44. data/lib/chef/providers/right_script_resource.rb +113 -0
  45. data/lib/chef/providers/rs_shutdown_provider.rb +75 -0
  46. data/lib/chef/providers/rs_shutdown_resource.rb +55 -0
  47. data/lib/chef/providers/server_collection_provider.rb +66 -0
  48. data/lib/chef/providers/server_collection_resource.rb +93 -0
  49. data/lib/chef/providers/windows/powershell_provider.rb +151 -0
  50. data/lib/chef/providers/windows/powershell_resource.rb +111 -0
  51. data/lib/chef/providers/windows/unsupported_provider.rb +51 -0
  52. data/lib/chef/right_providers.rb +55 -0
  53. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/ChefNodeCmdlet.csproj +104 -0
  54. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/ChefNodeCmdlet.dll-Help.xml +141 -0
  55. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/Exceptions.cs +182 -0
  56. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetChefNodeCommand.cs +58 -0
  57. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetChefNodeRequest.cs +46 -0
  58. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetChefNodeResponse.cs +45 -0
  59. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetCurrentResourceCommand.cs +58 -0
  60. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetCurrentResourceRequest.cs +46 -0
  61. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetCurrentResourceResponse.cs +45 -0
  62. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNewResourceCommand.cs +58 -0
  63. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNewResourceRequest.cs +46 -0
  64. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNewResourceResponse.cs +45 -0
  65. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNextActionCommand.cs +178 -0
  66. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNextActionRequest.cs +67 -0
  67. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNextActionResponse.cs +58 -0
  68. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNodeValueCommandBase.cs +142 -0
  69. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNodeValueRequestBase.cs +64 -0
  70. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/GetNodeValueResponseBase.cs +69 -0
  71. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/JsonTransport.cs +110 -0
  72. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/PipeClient.cs +158 -0
  73. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/PipeServer.cs +142 -0
  74. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/Properties/AssemblyInfo.cs +16 -0
  75. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/ProtocolConstants.cs +55 -0
  76. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/ProtocolUtilities.cs +77 -0
  77. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/ReadMe.txt +53 -0
  78. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetChefNodeCommand.cs +59 -0
  79. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetChefNodeRequest.cs +46 -0
  80. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetChefNodeResponse.cs +58 -0
  81. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetCurrentResourceCommand.cs +59 -0
  82. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetCurrentResourceRequest.cs +46 -0
  83. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetCurrentResourceResponse.cs +40 -0
  84. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetNewResourceCommand.cs +59 -0
  85. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetNewResourceRequest.cs +46 -0
  86. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetNewResourceResponse.cs +40 -0
  87. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetNodeValueCommandBase.cs +293 -0
  88. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetNodeValueRequestBase.cs +75 -0
  89. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/SetNodeValueResponseBase.cs +45 -0
  90. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet/Transport.cs +91 -0
  91. data/lib/chef/windows/ChefNodeCmdlet/ChefNodeCmdlet.sln +35 -0
  92. data/lib/chef/windows/ChefNodeCmdlet/TestChefNodeCmdlet/Program.cs +374 -0
  93. data/lib/chef/windows/ChefNodeCmdlet/TestChefNodeCmdlet/Properties/AssemblyInfo.cs +16 -0
  94. data/lib/chef/windows/ChefNodeCmdlet/TestChefNodeCmdlet/TestChefNodeCmdlet.csproj +65 -0
  95. data/lib/chef/windows/ChefNodeCmdlet/TestNextActionCmdlet/Program.cs +136 -0
  96. data/lib/chef/windows/ChefNodeCmdlet/TestNextActionCmdlet/Properties/AssemblyInfo.cs +36 -0
  97. data/lib/chef/windows/ChefNodeCmdlet/TestNextActionCmdlet/ReadMe.txt +46 -0
  98. data/lib/chef/windows/ChefNodeCmdlet/TestNextActionCmdlet/TestNextActionCmdlet.csproj +68 -0
  99. data/lib/chef/windows/bin/Newtonsoft.Json.dll +0 -0
  100. data/lib/chef/windows/chef_node_server.rb +463 -0
  101. data/lib/chef/windows/dynamic_powershell_provider.rb +296 -0
  102. data/lib/chef/windows/pipe_server.rb +283 -0
  103. data/lib/chef/windows/powershell_host.rb +285 -0
  104. data/lib/chef/windows/powershell_pipe_server.rb +136 -0
  105. data/lib/chef/windows/powershell_provider_base.rb +92 -0
  106. data/lib/chef/windows/scripts/run_loop.ps1 +105 -0
  107. data/lib/clouds/cloud.rb +557 -0
  108. data/lib/clouds/cloud_factory.rb +250 -0
  109. data/lib/clouds/cloud_utilities.rb +244 -0
  110. data/lib/clouds/clouds/azure.rb +106 -0
  111. data/lib/clouds/clouds/cloudstack.rb +114 -0
  112. data/lib/clouds/clouds/ec2.rb +113 -0
  113. data/lib/clouds/clouds/eucalyptus.rb +46 -0
  114. data/lib/clouds/clouds/google.rb +102 -0
  115. data/lib/clouds/clouds/none.rb +76 -0
  116. data/lib/clouds/clouds/openstack.rb +30 -0
  117. data/lib/clouds/clouds/rackspace-ng.rb +54 -0
  118. data/lib/clouds/clouds/rackspace.rb +78 -0
  119. data/lib/clouds/clouds/softlayer.rb +91 -0
  120. data/lib/clouds/metadata_formatter.rb +108 -0
  121. data/lib/clouds/metadata_provider.rb +128 -0
  122. data/lib/clouds/metadata_source.rb +87 -0
  123. data/lib/clouds/metadata_sources/certificate_metadata_source.rb +207 -0
  124. data/lib/clouds/metadata_sources/config_drive_metadata_source.rb +129 -0
  125. data/lib/clouds/metadata_sources/file_metadata_source.rb +74 -0
  126. data/lib/clouds/metadata_sources/http_metadata_source.rb +277 -0
  127. data/lib/clouds/metadata_sources/selective_metadata_source.rb +122 -0
  128. data/lib/clouds/metadata_tree_climber.rb +144 -0
  129. data/lib/clouds/metadata_writer.rb +155 -0
  130. data/lib/clouds/metadata_writers/dictionary_metadata_writer.rb +72 -0
  131. data/lib/clouds/metadata_writers/ruby_metadata_writer.rb +76 -0
  132. data/lib/clouds/metadata_writers/shell_metadata_writer.rb +121 -0
  133. data/lib/clouds/register_clouds.rb +34 -0
  134. data/lib/clouds.rb +32 -0
  135. data/lib/gem_dependencies.rb +83 -0
  136. data/lib/git_hooks/commit-msg.rb +7 -0
  137. data/lib/instance/agent_config.rb +168 -0
  138. data/lib/instance/agent_watcher.rb +233 -0
  139. data/lib/instance/audit_cook_stub.rb +104 -0
  140. data/lib/instance/audit_proxy.rb +247 -0
  141. data/lib/instance/bundle_queue.rb +104 -0
  142. data/lib/instance/cook/agent_connection.rb +109 -0
  143. data/lib/instance/cook/audit_logger.rb +165 -0
  144. data/lib/instance/cook/audit_stub.rb +142 -0
  145. data/lib/instance/cook/ca-bundle.crt +2794 -0
  146. data/lib/instance/cook/chef_state.rb +211 -0
  147. data/lib/instance/cook/cook.rb +306 -0
  148. data/lib/instance/cook/cook_state.rb +298 -0
  149. data/lib/instance/cook/cookbook_path_mapping.rb +66 -0
  150. data/lib/instance/cook/cookbook_repo_retriever.rb +190 -0
  151. data/lib/instance/cook/executable_sequence.rb +765 -0
  152. data/lib/instance/cook/external_parameter_gatherer.rb +190 -0
  153. data/lib/instance/cook/repose_downloader.rb +349 -0
  154. data/lib/instance/cook/shutdown_request_proxy.rb +121 -0
  155. data/lib/instance/cook.rb +41 -0
  156. data/lib/instance/downloader.rb +208 -0
  157. data/lib/instance/duplicable.rb +67 -0
  158. data/lib/instance/exceptions.rb +49 -0
  159. data/lib/instance/executable_sequence_proxy.rb +278 -0
  160. data/lib/instance/instance_commands.rb +577 -0
  161. data/lib/instance/instance_state.rb +633 -0
  162. data/lib/instance/json_utilities.rb +102 -0
  163. data/lib/instance/login_manager.rb +533 -0
  164. data/lib/instance/login_user_manager.rb +522 -0
  165. data/lib/instance/message_encoder.rb +118 -0
  166. data/lib/instance/multi_thread_bundle_queue.rb +232 -0
  167. data/lib/instance/operation_context.rb +60 -0
  168. data/lib/instance/options_bag.rb +65 -0
  169. data/lib/instance/payload_formatter.rb +46 -0
  170. data/lib/instance/policy.rb +53 -0
  171. data/lib/instance/policy_audit.rb +100 -0
  172. data/lib/instance/policy_manager.rb +146 -0
  173. data/lib/instance/reenroll_manager.rb +104 -0
  174. data/lib/instance/right_scripts_cookbook.rb +181 -0
  175. data/lib/instance/shutdown_request.rb +221 -0
  176. data/lib/instance/single_thread_bundle_queue.rb +189 -0
  177. data/lib/instance/volume_management.rb +450 -0
  178. data/lib/instance.rb +50 -0
  179. data/lib/repo_conf_generators/apt_conf_generators.rb +106 -0
  180. data/lib/repo_conf_generators/gem_conf_generators.rb +80 -0
  181. data/lib/repo_conf_generators/rightscale_conf_generators.rb +254 -0
  182. data/lib/repo_conf_generators/rightscale_key.pub +17 -0
  183. data/lib/repo_conf_generators/yum_conf_generators.rb +225 -0
  184. data/lib/repo_conf_generators.rb +30 -0
  185. data/lib/run_shell.rb +28 -0
  186. data/scripts/agent_checker.rb +571 -0
  187. data/scripts/agent_controller.rb +247 -0
  188. data/scripts/agent_deployer.rb +148 -0
  189. data/scripts/bundle_runner.rb +336 -0
  190. data/scripts/cloud_controller.rb +176 -0
  191. data/scripts/log_level_manager.rb +142 -0
  192. data/scripts/ohai_runner.rb +33 -0
  193. data/scripts/reenroller.rb +193 -0
  194. data/scripts/server_importer.rb +293 -0
  195. data/scripts/shutdown_client.rb +183 -0
  196. data/scripts/system_configurator.rb +367 -0
  197. data/scripts/tagger.rb +381 -0
  198. data/scripts/thunker.rb +356 -0
  199. metadata +418 -0
@@ -0,0 +1,367 @@
1
+ # === Synopsis:
2
+ # RightScale System Configuration Utility (system) - (c) 2011 RightScale Inc
3
+ #
4
+ # This utility performs miscellaneous system configuration tasks.
5
+ #
6
+ # === Examples:
7
+ # system --action=hostname
8
+ # system --action=ssh
9
+ # system --action=proxy
10
+ #
11
+ # === Usage
12
+ # system --action=<action> [options]
13
+ #
14
+ # Options:
15
+ # --help: Display help
16
+ #
17
+
18
+ require 'trollop'
19
+ require 'socket'
20
+
21
+ require 'right_agent'
22
+ require 'right_agent/scripts/usage'
23
+ require 'right_agent/scripts/common_parser'
24
+
25
+ # RightLink dependencies
26
+ require File.normalize_path(File.join(File.dirname(__FILE__), '..', 'lib', 'instance', 'agent_config'))
27
+
28
+ cloud_dir = RightScale::AgentConfig.cloud_state_dir
29
+
30
+ begin
31
+ require File.join(cloud_dir, 'meta-data-cache')
32
+ rescue LoadError => e
33
+ puts "No cloud metadata is available on this machine - some modules may not work correctly!"
34
+ end
35
+
36
+ begin
37
+ require File.join(cloud_dir, 'user-data')
38
+ rescue LoadError => e
39
+ puts "No cloud user-data is available on this machine - some modules may not work correctly!"
40
+ end
41
+
42
+ module RightScale
43
+ class SystemConfigurator
44
+ RSA_KEY = File.join(RightScale::Platform.filesystem.ssh_cfg_dir, 'ssh_host_rsa_key')
45
+ DSA_KEY = File.join(RightScale::Platform.filesystem.ssh_cfg_dir, 'ssh_host_dsa_key')
46
+ SUDO_USER = 'rightscale'
47
+ SUDO_GROUP = 'rightscale_sudo'
48
+
49
+ def read_options_file
50
+ state = RightScale::Platform.filesystem.right_link_dynamic_state_dir
51
+ options_file = File.join(state, 'system.js')
52
+ old_options_file = File.join(state, 'sys_configure.js')
53
+
54
+ if File.readable?(options_file)
55
+ return File.read(options_file)
56
+ elsif File.readable?(old_options_file)
57
+ return File.read(old_options_file)
58
+ else
59
+ return nil
60
+ end
61
+ end
62
+
63
+ def self.run
64
+ configurator = SystemConfigurator.new
65
+ configurator.start(configurator.parse_args)
66
+ rescue SystemExit => e
67
+ raise e
68
+ rescue Exception => e
69
+ fail(e)
70
+ end
71
+
72
+ def start(options)
73
+ if (json = read_options_file)
74
+ options.merge(JSON.load(json))
75
+ else
76
+ all_actions = methods.select { |m| m =~ /^configure_/ }.map { |m| m[10..-1] }
77
+ options.merge({'actions_enabled' => all_actions})
78
+ end
79
+
80
+ if options[:action]
81
+ actions = [ options[:action] ]
82
+ else
83
+ actions = []
84
+ end
85
+
86
+ if actions.empty?
87
+ raise StandardError, "No action specified; try --help"
88
+ end
89
+
90
+ actions.each do |action|
91
+ method_name = "configure_#{action}".to_sym
92
+ if action && respond_to?(method_name)
93
+ puts "Configuring #{action}"
94
+ __send__(method_name)
95
+ else
96
+ raise StandardError, "Unknown action #{action}"
97
+ end
98
+ end
99
+
100
+ return 0
101
+ end
102
+
103
+ # Create options hash from command line arguments
104
+ #
105
+ # === Return
106
+ # options(Hash):: Hash of options as defined by the command line
107
+ def parse_args
108
+ parser = Trollop::Parser.new do
109
+ opt :action, "", :type => :string
110
+ end
111
+
112
+ begin
113
+ parser.parse
114
+ rescue Trollop::HelpNeeded
115
+ puts Usage.scan(__FILE__)
116
+ exit
117
+ rescue Trollop::CommandlineError => e
118
+ puts e.message + "\nUse --help for additional information"
119
+ exit(1)
120
+ rescue SystemExit => e
121
+ raise e
122
+ end
123
+ end
124
+
125
+ def configure_ssh
126
+ return 0 unless Platform.linux?
127
+
128
+ puts "Freshening SSH host keys to ensure they are unique to this instance..."
129
+
130
+ if File.readable?(RSA_KEY)
131
+ replace_key(RSA_KEY, 'rsa')
132
+ puts "* replaced RSA key"
133
+ changed = true
134
+ else
135
+ puts "* RSA key does not exist"
136
+ end
137
+
138
+ if File.readable?(DSA_KEY)
139
+ replace_key(DSA_KEY, 'dsa')
140
+ puts "* replaced DSA key"
141
+ changed = true
142
+ else
143
+ puts "* DSA key does not exist"
144
+ end
145
+
146
+ if changed
147
+ restart_sshd
148
+ end
149
+
150
+ return 0
151
+ end
152
+
153
+ def configure_hostname
154
+ return 0 unless Platform.linux?
155
+
156
+ hostname = Socket.gethostname
157
+ current_fqdn = valid_current_fqdn
158
+
159
+ if current_fqdn == nil
160
+ # We do not have a valid FQDN; some work is required
161
+ puts "Hostname (#{current_fqdn.inspect}) looks suspect; changing it"
162
+ cloud_fqdn, cloud_ip = retrieve_cloud_hostname_and_local_ip
163
+ set_hostname(cloud_fqdn, cloud_ip)
164
+
165
+ # Check if setting the hostname has caused FQDN to work, before
166
+ # adding a fake entry to /etc/hosts as a last resort
167
+ add_host_record(cloud_fqdn, cloud_ip) unless valid_current_fqdn
168
+ else
169
+ # If hostname is already fully-qualified, then do nothing
170
+ puts "Hostname (#{current_fqdn.inspect}) is a well-formed and valid FQDN."
171
+ end
172
+ end
173
+
174
+ def configure_proxy
175
+ return 0 unless Platform.linux?
176
+
177
+ unset_proxy_variables
178
+
179
+ if ENV['RS_HTTP_PROXY']
180
+ puts "Configuring HTTP proxy #{ENV['$RS_HTTP_PROXY']}"
181
+
182
+ # TODO: super hack for open-uri
183
+ # fix it
184
+ proxy_uri = URI.parse("http://" + ENV['RS_HTTP_PROXY'])
185
+
186
+ unless proxy_uri.host && proxy_uri.port
187
+ puts "Proxy specifier is malformed (must contain 'host:port'); skipping proxy."
188
+ return
189
+ end
190
+
191
+ # Requests to the metadata server should never be proxied. Detect where our
192
+ # metadata server lives and add this to the no-proxy list automatically.
193
+ no_proxy = get_proxy_exclude_list
194
+
195
+ #create global subversion servers config
196
+ create_subversion_servers_config(proxy_uri, no_proxy)
197
+
198
+ #create profile.d entry for http_proxy and no_proxy
199
+ create_proxy_profile_script(proxy_uri, no_proxy)
200
+ else
201
+ puts "Proxy settings not found in userdata; continuing without."
202
+ end
203
+ end
204
+
205
+ protected
206
+
207
+ def runshell(command)
208
+ puts "+ #{command}"
209
+ output = `#{command} < /dev/null 2>&1`
210
+ raise StandardError, "Command failure: #{output}" unless $?.success?
211
+ end
212
+
213
+ def replace_key(private_key_file, algorithm)
214
+ public_key_file = "#{private_key_file}.pub"
215
+
216
+ puts "Regenerating #{private_key_file}"
217
+ FileUtils.rm(private_key_file) if File.exist?(private_key_file)
218
+ FileUtils.rm(public_key_file) if File.exist?(public_key_file)
219
+ runshell("ssh-keygen -f #{private_key_file} -t #{algorithm} -N ''")
220
+ end
221
+
222
+ def restart_sshd
223
+ sshd_name = File.exist?('/etc/init.d/sshd') ? "sshd" : "ssh"
224
+ puts "Restarting SSHD..."
225
+ runshell("/etc/init.d/#{sshd_name} restart")
226
+ end
227
+
228
+ def retrieve_cloud_hostname_and_local_ip
229
+ # Cloud-specific case: query EC2/Eucalyptus metadata to learn local
230
+ # hostname and local public IP address
231
+ if Platform.ec2? || Platform.eucalyptus?
232
+ my_fqdn = ENV['EC2_LOCAL_HOSTNAME']
233
+ my_addr = ENV['EC2_LOCAL_IPV4']
234
+
235
+ # Some clouds are buggy and report an IP address as EC2_LOCAL_HOSTNAME.
236
+ # An IP address is not a valid hostname! In this case we must transform
237
+ # it to a valid hostname using the form ip-x-y-z-w where x,y,z,w are
238
+ # the decimal octets of the IP address x.y.z.w
239
+ if my_fqdn =~ /^[0-9.]+$/
240
+ components = my_fqdn.split('.')
241
+ my_fqdn = "ip-#{components.join('-')}.internal"
242
+ end
243
+ end
244
+
245
+ # Generic case: use existing hostname and append fake "internal" suffix
246
+ unless my_fqdn
247
+ my_fqdn ||= "#{Socket.gethostname}.internal"
248
+ end
249
+
250
+ unless my_addr
251
+ bdns, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
252
+ begin
253
+ # Generic case: create a UDP "connection" to our hostname
254
+ # and look at socket data to determine local IP address.
255
+ my_addr = UDPSocket.open do |socket|
256
+ socket.connect(Socket.gethostname, 8000)
257
+ socket.addr.last
258
+ end
259
+ rescue Exception => e
260
+ # Absolute last-ditch effort: use localhost IP.
261
+ # Not ideal, but at least it works...
262
+ my_addr = '127.0.0.1'
263
+ ensure
264
+ Socket.do_not_reverse_lookup = bdns
265
+ end
266
+ end
267
+
268
+ [ my_fqdn, my_addr ]
269
+ end
270
+
271
+ def valid_current_fqdn
272
+ hostname_f = Socket.gethostbyname(Socket.gethostname)[0] rescue nil
273
+ if hostname_f && hostname_f.include?('.')
274
+ hostname_f
275
+ else
276
+ nil
277
+ end
278
+ end
279
+
280
+ def set_hostname(my_fqdn, my_addr)
281
+ hostname = my_fqdn.split(".").first
282
+ # Set our hostname to the host portion of the FQDN
283
+ runshell("hostname #{hostname}")
284
+ runshell("echo #{hostname} > /etc/hostname")
285
+ puts "Changed hostname to #{hostname}"
286
+ end
287
+
288
+ def add_host_record(my_fqdn, my_addr)
289
+ hostname = my_fqdn.split('.').first
290
+ mask = Regexp.new(Regexp.escape(hostname))
291
+
292
+ begin
293
+ lines = File.readlines('/etc/hosts')
294
+ hosts_file = File.open("/etc/hosts", "w")
295
+ lines.each { |line| hosts_file.puts line.strip unless line =~ mask}
296
+ hosts_file.puts("#{my_addr} #{my_fqdn} #{hostname}")
297
+ hosts_file.close
298
+ end
299
+ puts "Added FQDN hostname entry (#{my_fqdn}) to /etc/hosts"
300
+ end
301
+
302
+ def get_proxy_exclude_list
303
+ no_proxy = []
304
+
305
+ if Platform.eucalyptus?
306
+ meta_server = IPSocket.getaddress(euca_metadata) rescue '169.254.169.254'
307
+ no_proxy << meta_server
308
+ else
309
+ #a reasonable default, e.g. for EC2 and for some CloudStack/OpenStack
310
+ #configurations
311
+ no_proxy << '169.254.169.254'
312
+ end
313
+
314
+ #parse "skip proxy for these servers" setting out of metadata element
315
+ if ENV['RS_NO_PROXY']
316
+ no_proxy = no_proxy + ENV['RS_NO_PROXY'].split(',')
317
+ end
318
+
319
+ no_proxy
320
+ end
321
+
322
+ def create_subversion_servers_config(proxy_uri, no_proxy_list)
323
+ subversion_servers_path = '/etc/subversion/servers'
324
+ File.open(subversion_servers_path, 'w') do |f|
325
+ f.puts '[global]'
326
+
327
+ if proxy_uri && proxy_uri.host && proxy_uri.port
328
+ f.puts "http-proxy-host = #{proxy_uri.host}"
329
+ f.puts "http-proxy-port = #{proxy_uri.port}"
330
+ end
331
+
332
+ if no_proxy_list && no_proxy_list.size > 0
333
+ f.puts "http-proxy-exceptions = #{no_proxy_list.join(',')}"
334
+ end
335
+ end
336
+ end
337
+
338
+ def create_proxy_profile_script(proxy_uri, no_proxy_list)
339
+ sript_path = '/etc/profile.d/http_proxy.sh'
340
+
341
+ File.open(sript_path, 'w') do |f|
342
+ f.puts "# Settings auto-generated by RightScale. Do not change unless you know what"
343
+ f.puts "# you're doing."
344
+
345
+ http_proxy = "http_proxy"
346
+ https_proxy = "https_proxy"
347
+ no_proxy = "no_proxy"
348
+
349
+ if proxy_uri && proxy_uri.host && proxy_uri.port
350
+ [http_proxy, https_proxy, http_proxy.upcase, https_proxy.upcase].each do |variable|
351
+ f.puts "export #{variable}=\"http://#{proxy_uri.host}:#{proxy_uri.port}\""
352
+ end
353
+ end
354
+
355
+ if no_proxy_list && no_proxy_list.size > 0
356
+ [no_proxy, no_proxy.upcase].each do |variable|
357
+ f.puts "export #{variable}=\"#{no_proxy_list.join(',')}\""
358
+ end
359
+ end
360
+ end
361
+ end
362
+
363
+ def unset_proxy_variables
364
+ runshell("unset http_proxy ; unset HTTP_PROXY ; unset no_proxy; unset NO_PROXY")
365
+ end
366
+ end
367
+ end