Module: NexusSW::LXD::Transport::Mixins::CLI

Includes:
Helpers::UploadFolder
Included in:
CLI
Defined in:
lib/nexussw/lxd/transport/mixins/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::UploadFolder

#upload_files_individually, #upload_using_tarball

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 18

def config
  @config
end

#container_nameObject (readonly)

Returns the value of attribute container_name.



18
19
20
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 18

def container_name
  @container_name
end

#inner_transportObject (readonly)

Returns the value of attribute inner_transport.



18
19
20
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 18

def inner_transport
  @inner_transport
end

#puntObject (readonly)

Returns the value of attribute punt.



18
19
20
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 18

def punt
  @punt
end

Instance Method Details

#add_remote(host_name) ⇒ Object



74
75
76
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 74

def add_remote(host_name)
  execute("add #{host_name} --accept-certificate", subcommand: 'remote').error! unless remote? host_name
end

#download_file(path, local_path) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 51

def download_file(path, local_path)
  tfile = inner_mktmp if punt
  localname = tfile || local_path
  execute("#{container_name}#{path} #{localname}", subcommand: 'file pull', capture: false).error!
  inner_transport.download_file tfile, local_path if tfile
ensure
  inner_transport.execute("rm -rf #{tfile}", capture: false) if tfile
end

#execute(command, options = {}, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 22

def execute(command, options = {}, &block)
  mycommand = command.is_a?(Array) ? command.join(' ') : command
  subcommand = options[:subcommand] || "exec #{container_name} --"
  mycommand = "lxc #{subcommand} #{mycommand}"
  options = options.except :subcommand if options.key? :subcommand
  # We would have competing timeout logic depending on who the inner transport is
  # I'll just let rest & local do the timeouts, and if inner is a chef sourced transport, they have timeout logic of their own
  # with_timeout_and_retries(options) do
  inner_transport.execute mycommand, options, &block
end

#initialize(remote_transport, container_name, config = {}) ⇒ Object



11
12
13
14
15
16
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 11

def initialize(remote_transport, container_name, config = {})
  @container_name = container_name
  @config = config
  @inner_transport = remote_transport
  @punt = !inner_transport.is_a?(::NexusSW::LXD::Transport::Mixins::Local)
end

#linked_transport(host_name) ⇒ Object



78
79
80
81
82
83
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 78

def linked_transport(host_name)
  linked = inner_transport.linked_transport(host_name) if inner_transport.is_a?(::NexusSW::LXD::Transport::CLI)
  return linked if linked
  return nil unless remote?(host_name)
  new(driver, inner_transport, "#{host_name}:#{container_name}", config)
end

#read_file(path) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 33

def read_file(path)
  tfile = inner_mktmp
  retval = execute("#{@container_name}#{path} #{tfile}", subcommand: 'file pull', capture: false)
  # return '' if retval.exitstatus == 1
  retval.error!
  return inner_transport.read_file tfile
ensure
  inner_transport.execute("rm -rf #{tfile}", capture: false) if tfile
end

#remote?(host_name) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 85

def remote?(host_name)
  result = execute 'list', subcommand: 'remote'
  result.error!
  result.stdout.each_line do |line|
    return true if line.start_with? "| #{host_name} "
  end
  false
end

#upload_file(local_path, path) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 60

def upload_file(local_path, path)
  tfile = inner_mktmp if punt
  localname = tfile || local_path
  inner_transport.upload_file local_path, tfile if tfile
  execute("#{localname} #{container_name}#{path}", subcommand: 'file push', capture: false).error!
ensure
  inner_transport.execute("rm -rf #{tfile}", capture: false) if tfile
end

#upload_folder(local_path, path) ⇒ Object



69
70
71
72
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 69

def upload_folder(local_path, path)
  return super unless config[:info] && config[:info]['api_extensions'] && config[:info]['api_extensions'].include?('directory_manipulation')
  execute("-r #{local_path} #{container_name}#{path}", subcommand: 'file push', capture: false).error!
end

#write_file(path, content) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 43

def write_file(path, content)
  tfile = inner_mktmp
  inner_transport.write_file tfile, content
  execute("#{tfile} #{container_name}#{path}", subcommand: 'file push', capture: false).error!
ensure
  inner_transport.execute("rm -rf #{tfile}", capture: false) if tfile
end