Class: Bosh::Agent::Infrastructure::Vsphere::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_agent/infrastructure/vsphere/settings.rb

Constant Summary collapse

DEFAULT_CDROM_RETRY_WAIT =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSettings

Returns a new instance of Settings.



9
10
11
12
13
14
15
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 9

def initialize
  base_dir = Bosh::Agent::Config.base_dir
  @logger = Bosh::Agent::Config.logger
  @cdrom_retry_wait = DEFAULT_CDROM_RETRY_WAIT
  @cdrom_settings_mount_point = File.join(base_dir, 'bosh', 'settings')
  @cdrom_device = nil
end

Instance Attribute Details

#cdrom_retry_waitObject

Returns the value of attribute cdrom_retry_wait.



7
8
9
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 7

def cdrom_retry_wait
  @cdrom_retry_wait
end

Instance Method Details

#cdrom_deviceObject



17
18
19
20
21
22
23
24
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 17

def cdrom_device
  unless @cdrom_device
    # only do this when not already done
    cd_drive = File.read("/proc/sys/dev/cdrom/info").slice(/drive name:\s*\S*/).slice(/\S*\z/)
    @cdrom_device = "/dev/#{cd_drive.strip}"
  end
  @cdrom_device
end

#check_cdromObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 49

def check_cdrom
  # Below we do a number of retries to work around race conditions both
  # when inserting a virtual cdrom in vSphere and how udev handles
  # detection of the new media.
  #
  # Part of what the udev cdrom-id helper will do is to open /dev/cdrom
  # with O_EXCL, which will give us EBUSY - however, the exact timing of
  # this is a little harder - so we retry.

  # First give the cdrom media a little time to get detected
  5.times do
    begin
      read_cdrom_byte
      break
    rescue => e
      @logger.info("Waiting for #{cdrom_device} (ENOMEDIUM): #{e.inspect}")
    end
    sleep @cdrom_retry_wait
  end

  # Second read - invoke udevadmin settle
  begin
    read_cdrom_byte
  rescue => e
    # Do nothing
  end

  begin
    # udevadm settle default timeout is 120 seconds
    udevadm_settle_out = udevadm_settle
    @logger.info("udevadm: #{udevadm_settle_out}")
  rescue
    raise Bosh::Agent::LoadSettingsError, "udevadm failed: #{e.inspect}"
  end

  # Read successfuly from cdrom for 2.5 seconds
  5.times do
    begin
      read_cdrom_byte
    rescue Errno::EBUSY
      @logger.info("Waiting for udev cdrom-id (EBUSY)")
      # do nothing
    rescue Errno::ENOMEDIUM # 1.8: Errno::E123
      @logger.info("Waiting for #{cdrom_device} (ENOMEDIUM or ENOTBLK)")
      # do nothing
    end
    sleep @cdrom_retry_wait
  end

  begin
    read_cdrom_byte
  rescue Errno::EBUSY, Errno::ENOMEDIUM # 1.8: Errno::E123
    raise Bosh::Agent::LoadSettingsError, "No bosh cdrom env: #{e.inspect}"
  end
end

#create_cdrom_settings_mount_pointObject



119
120
121
122
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 119

def create_cdrom_settings_mount_point
  FileUtils.mkdir_p(@cdrom_settings_mount_point)
  FileUtils.chmod(0700, @cdrom_settings_mount_point)
end

#eject_cdromObject



134
135
136
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 134

def eject_cdrom
  Bosh::Exec.sh "eject #{cdrom_device}"
end

#load_cdrom_settingsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 30

def load_cdrom_settings
  check_cdrom
  create_cdrom_settings_mount_point
  mount_cdrom

  env_file = File.join(@cdrom_settings_mount_point, 'env')

  begin
    settings_json = File.read(env_file)
    @settings = Yajl::Parser.new.parse(settings_json)
  rescue
    raise Bosh::Agent::LoadSettingsError, 'Failed to read/write env/settings.json'
  ensure
    umount_cdrom
    eject_cdrom
  end
  @settings
end

#load_settingsObject



26
27
28
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 26

def load_settings
  load_cdrom_settings
end

#mount_cdromObject



124
125
126
127
128
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 124

def mount_cdrom
  result = Bosh::Exec.sh "mount #{cdrom_device} #@cdrom_settings_mount_point 2>&1"
  raise Bosh::Agent::LoadSettingsError,
    "Failed to mount settings on #@cdrom_settings_mount_point: #{result.output}" if result.failed?
end

#read_cdrom_byteObject



115
116
117
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 115

def read_cdrom_byte
  File.read(cdrom_device, 1)
end

#udevadm_settleObject



105
106
107
108
109
110
111
112
113
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 105

def udevadm_settle
  if File.exists? "/sbin/udevadm"
    Bosh::Exec.sh "/sbin/udevadm settle"
  elsif File.exists? "/sbin/udevsettle"
    Bosh::Exec.sh "/sbin/udevsettle"
  else
    raise Bosh::Agent::LoadSettingsError, "No udevsettle"
  end
end

#umount_cdromObject



130
131
132
# File 'lib/bosh_agent/infrastructure/vsphere/settings.rb', line 130

def umount_cdrom
  Bosh::Exec.sh "umount #@cdrom_settings_mount_point 2>&1"
end