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

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

Instance Attribute Summary collapse

Attributes included from Helpers::UsersMixin

#file_mode, #gid, #uid, #username

Instance Method Summary collapse

Methods included from Helpers::UsersMixin

#reset_user, #user

Methods included from Helpers::FolderTxfr

#download_files_individually, #download_using_tarball, #upload_files_individually, #upload_using_tarball

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#container_nameObject (readonly)

Returns the value of attribute container_name.



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

def container_name
  @container_name
end

#inner_transportObject (readonly)

Returns the value of attribute inner_transport.



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

def inner_transport
  @inner_transport
end

#puntObject (readonly)

Returns the value of attribute punt.



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

def punt
  @punt
end

Instance Method Details

#add_remote(host_name) ⇒ Object



107
108
109
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 107

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



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

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

#download_folder(path, local_path, options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 93

def download_folder(path, local_path, options = {})
  return super unless config[:info] && config[:info]["api_extensions"] && config[:info]["api_extensions"].include?("directory_manipulation")

  puntname = local_path
  if punt
    tfile = Transport.remote_tempname(container_name)
    puntname = File.join(tfile, File.basename(path))
  end
  execute("#{container_name}#{path} #{tfile || local_path}", subcommand: "file pull -r", capture: false).error!
  inner_transport.download_folder(puntname, local_path) if punt
ensure
  inner_transport.execute("rm -rf #{tfile}", capture: false) if tfile
end

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 24

def execute(command, options = {}, &block)
  command = runas_command(command, options) unless options[:subcommand]
  command = command.shelljoin if command.is_a?(Array)
  subcommand = options[:subcommand]
  unless subcommand
    subcommand = "exec #{container_name} --"
    # command = ['bash', '-c', command].shelljoin
  end
  command = "lxc #{subcommand} #{command}"

  options = options.reject { |k, _| [:subcommand, :runas].include? k }

  inner_transport.execute command, options, &block
end

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



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

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



111
112
113
114
115
116
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 111

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



39
40
41
42
43
44
45
46
47
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 39

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

#remote?(host_name) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 118

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, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 68

def upload_file(local_path, path, options = {})
  perms = file_perms(options)

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

#upload_folder(local_path, path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/nexussw/lxd/transport/mixins/cli.rb', line 79

def upload_folder(local_path, path)
  return super unless config[:info] && config[:info]["api_extensions"] && config[:info]["api_extensions"].include?("directory_manipulation")

  puntname = local_path
  if punt
    tfile = Transport.remote_tempname(container_name)
    puntname = File.join(tfile, File.basename(local_path))
    inner_transport.upload_folder(local_path, tfile)
  end
  execute("#{puntname} #{container_name}#{path}", subcommand: "file push -r", capture: false).error!
ensure
  inner_transport.execute("rm -rf #{tfile}", capture: false) if tfile
end

#write_file(path, content, options = {}) ⇒ Object



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

def write_file(path, content, options = {})
  perms = file_perms(options)

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