Class: Bosh::Agent::Util
- Includes:
- Exec
- Defined in:
- lib/bosh_agent/util.rb
Class Method Summary collapse
- .base_dir ⇒ Object
- .block_device_size(block_device) ⇒ Object
-
.config_binding(spec) ⇒ Binding
Template evaluation binding.
- .create_symlink(src, dst) ⇒ Object
- .get_network_info ⇒ Object
- .lazy_itable_init_enabled? ⇒ Boolean
- .logger ⇒ Object
- .partition_disk(dev, sfdisk_input) ⇒ Object
- .run_hook(hook, job_template_name) ⇒ Object
- .unpack_blob(blobstore_id, sha1, install_path) ⇒ Object
-
.update_file(data, path) ⇒ Object
Poor mans idempotency.
Class Method Details
.base_dir ⇒ Object
8 9 10 |
# File 'lib/bosh_agent/util.rb', line 8 def base_dir Bosh::Agent::Config.base_dir end |
.block_device_size(block_device) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/bosh_agent/util.rb', line 82 def block_device_size(block_device) unless File.blockdev?(block_device) raise Bosh::Agent::MessageHandlerError, "Not a blockdevice" end result = sh("/sbin/sfdisk -s #{block_device} 2>&1") unless result.output.match(/\A\d+\Z/) raise Bosh::Agent::MessageHandlerError, "Unable to determine disk size" end result.output.to_i end |
.config_binding(spec) ⇒ Binding
Returns Template evaluation binding.
63 64 65 |
# File 'lib/bosh_agent/util.rb', line 63 def config_binding(spec) Bosh::Common::TemplateEvaluationContext.new(spec).get_binding end |
.create_symlink(src, dst) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/bosh_agent/util.rb', line 133 def create_symlink(src, dst) # FileUtils doesn have 'no-deference' for links - causing ln_sf to # attempt to create target link in dst rather than to overwrite it. # BROKEN: FileUtils.ln_sf(src, dst) out = %x(ln -nsf #{src} #{dst} 2>&1) unless $?.exitstatus == 0 raise Bosh::Agent::MessageHandlerError, "Failed to link '#{src}' to '#{dst}': #{out}" end end |
.get_network_info ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/bosh_agent/util.rb', line 174 def get_network_info sigar = SigarBox.create_sigar net_info = sigar.net_info ifconfig = sigar.net_interface_config(net_info.default_gateway_interface) properties = {} properties["ip"] = ifconfig.address properties["netmask"] = ifconfig.netmask properties["gateway"] = net_info.default_gateway properties end |
.lazy_itable_init_enabled? ⇒ Boolean
78 79 80 |
# File 'lib/bosh_agent/util.rb', line 78 def lazy_itable_init_enabled? File.exists?("/sys/fs/ext4/features/lazy_itable_init") end |
.logger ⇒ Object
12 13 14 |
# File 'lib/bosh_agent/util.rb', line 12 def logger Bosh::Agent::Config.logger end |
.partition_disk(dev, sfdisk_input) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/bosh_agent/util.rb', line 67 def partition_disk(dev, sfdisk_input) if File.blockdev?(dev) sfdisk_cmd = "echo \"#{sfdisk_input}\" | sfdisk -uM #{dev}" output = %x[#{sfdisk_cmd}] unless $? == 0 logger.info("failed to parition #{dev}") logger.info(output) end end end |
.run_hook(hook, job_template_name) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bosh_agent/util.rb', line 95 def run_hook(hook, job_template_name) hook_file = File.join(base_dir, 'jobs', job_template_name, 'bin', hook) unless File.exists?(hook_file) return nil end unless File.executable?(hook_file) raise Bosh::Agent::MessageHandlerError, "`#{hook}' hook for `#{job_template_name}' job is not an executable file" end env = { 'PATH' => '/usr/sbin:/usr/bin:/sbin:/bin', } stdout_rd, stdout_wr = IO.pipe stderr_rd, stderr_wr = IO.pipe Process.spawn(env, hook_file, :out => stdout_wr, :err => stderr_wr, :unsetenv_others => true) Process.wait exit_status = $?.exitstatus stdout_wr.close stderr_wr.close result = stdout_rd.read error_output = stderr_rd.read logger.info("Hook #{hook} for job #{job_template_name}: #{result}") unless exit_status == 0 = "Hook #{hook} for #{job_template_name} failed " += "(exit: #{exit_status}) " += " stderr: #{error_output}, stdout: #{result}" logger.info() raise Bosh::Agent::MessageHandlerError, end result end |
.unpack_blob(blobstore_id, sha1, install_path) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bosh_agent/util.rb', line 16 def unpack_blob(blobstore_id, sha1, install_path) = Bosh::Agent::Config. bsc_provider = Bosh::Agent::Config.blobstore_provider blobstore_client = Bosh::Blobstore::Client.safe_create(bsc_provider, ) data_tmp = File.join(base_dir, 'data', 'tmp') FileUtils.mkdir_p(data_tmp) begin tf = Tempfile.open(blobstore_id, data_tmp) begin logger.info("Retrieving blob '#{blobstore_id}'") blobstore_client.get(blobstore_id, tf, sha1: sha1) rescue Bosh::Blobstore::BlobstoreError => e raise Bosh::Agent::MessageHandlerError.new(e.inspect) end partial_install_path = "#{install_path}-bosh-agent-unpack" logger.info("Creating '#{partial_install_path}'") FileUtils.rm_rf(partial_install_path) FileUtils.mkdir_p(partial_install_path) logger.info("Installing to '#{partial_install_path}'") Dir.chdir(partial_install_path) do output = `tar --no-same-owner -zxvf #{tf.path} 2>&1` unless $?.exitstatus == 0 raise Bosh::Agent::MessageHandlerError.new('Failed to unpack blob', output) end end # Only move contents of the blob to install path at the end # to avoid corrupted package directory FileUtils.mv(partial_install_path, install_path) rescue Exception => e logger.info("Failure unpacking blob: #{e.inspect} #{e.backtrace}") raise e ensure if tf tf.close tf.unlink end end end |
.update_file(data, path) ⇒ Object
Poor mans idempotency
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/bosh_agent/util.rb', line 145 def update_file(data, path) name = File.basename(path) dir = File.dirname(path) if_tmp_file = Tempfile.new(name, dir) if_tmp_file.write(data) if_tmp_file.flush old = nil begin old = Digest::SHA1.file(path).hexdigest rescue Errno::ENOENT logger.debug("missing file: #{path}") end new = Digest::SHA1.file(if_tmp_file.path).hexdigest updated = false unless old == new FileUtils.cp(if_tmp_file.path, path) updated = true end updated ensure if if_tmp_file if_tmp_file.close FileUtils.rm_rf(if_tmp_file.path) end end |