Module: Bosh::WardenCloud::Helpers

Included in:
Cloud, DiskUtils
Defined in:
lib/cloud/warden/helpers.rb

Constant Summary collapse

DEFAULT_SETTINGS_FILE =
'/var/vcap/bosh/settings.json'

Instance Method Summary collapse

Instance Method Details

#agent_settings_fileObject



41
42
43
# File 'lib/cloud/warden/helpers.rb', line 41

def agent_settings_file
  DEFAULT_SETTINGS_FILE
end

#cloud_error(message) ⇒ Object

Raises:

  • (Bosh::Clouds::CloudError)


7
8
9
10
# File 'lib/cloud/warden/helpers.rb', line 7

def cloud_error(message)
  @logger.error(message) if @logger
  raise Bosh::Clouds::CloudError, message
end

#generate_agent_env(vm_id, agent_id, networks, environment) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cloud/warden/helpers.rb', line 45

def generate_agent_env(vm_id, agent_id, networks, environment)
  vm_env = {
      'name' => vm_id,
      'id' => vm_id
  }

  env = {
      'vm' => vm_env,
      'agent_id' => agent_id,
      'networks' => networks,
      'disks' => { 'persistent' => {} },
  }
  env['env'] = environment if environment
  env.merge!(@agent_properties)
  env
end

#get_agent_env(handle) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cloud/warden/helpers.rb', line 62

def get_agent_env(handle)
  body = with_warden do |client|
    request = Warden::Protocol::RunRequest.new
    request.handle = handle
    request.privileged = true
    request.script = "cat #{agent_settings_file}"
    client.call(request).stdout
  end
  env = Yajl::Parser.parse(body)
  env
end

#set_agent_env(handle, env) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cloud/warden/helpers.rb', line 74

def set_agent_env(handle, env)
  tempfile = File.new("/tmp/agent-setting-#{Time.now.to_f}-#{Kernel.rand(100_000)}",'w')
  tempfile.write(Yajl::Encoder.encode(env))
  tempfile_in = "/tmp/#{Kernel.rand(100_000)}"
  tempfile.close
  # Here we copy the setting file to temp file in container, then mv it to
  # /var/vcap/bosh by privileged user.
  with_warden do |client|
    request = Warden::Protocol::CopyInRequest.new
    request.handle = handle
    request.src_path = tempfile.path
    request.dst_path = tempfile_in
    client.call(request)

    request = Warden::Protocol::RunRequest.new
    request.handle = handle
    request.privileged = true
    request.script = "mv #{tempfile_in} #{agent_settings_file}"
    client.call(request)
  end
end

#sh(cmd, su = false) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/cloud/warden/helpers.rb', line 24

def sh(cmd, su = false)
  runcmd = su == true ? "sudo -n #{cmd}" : cmd
  @logger.info "run '#{runcmd}'" if @logger
  Bosh::Exec.sh("#{runcmd}", yield: :on_false) do |result|
    yield result if block_given?
  end
end

#start_agent(handle) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/cloud/warden/helpers.rb', line 96

def start_agent(handle)
  with_warden do |client|
    request = Warden::Protocol::SpawnRequest.new
    request.handle = handle
    request.privileged = true
    request.script = '/usr/sbin/runsvdir-start'
    client.call(request)
  end
end

#sudo(cmd) ⇒ Object



20
21
22
# File 'lib/cloud/warden/helpers.rb', line 20

def sudo(cmd)
  sh(cmd, true)
end

#uuid(klass = nil) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/cloud/warden/helpers.rb', line 12

def uuid(klass = nil)
  id = SecureRandom.uuid
  if klass
    id = sprintf('%s-%s', klass, id)
  end
  id
end

#with_wardenObject



32
33
34
35
36
37
38
39
# File 'lib/cloud/warden/helpers.rb', line 32

def with_warden
  client = Warden::Client.new(@warden_unix_path)
  client.connect
  ret = yield client
  ret
ensure
  client.disconnect if client
end