Class: TrainPlugins::TrainKubernetes::File::Linux

Inherits:
Train::File::Remote::Linux
  • Object
show all
Defined in:
lib/train-kubernetes-docs/file/linux.rb

Instance Method Summary collapse

Constructor Details

#initialize(backend, path, follow_symlink = true, pod:, **args) ⇒ Linux

Returns a new instance of Linux.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/train-kubernetes-docs/file/linux.rb', line 8

def initialize(backend, path, follow_symlink = true, pod:, **args)
  @backend = backend
  @path = path || ''
  @follow_symlink = follow_symlink
  @pod = pod
  @container = args[:container]
  @namespace = args[:namespace]

  sanitize_filename(path)
  super(backend, path, follow_symlink)
end

Instance Method Details

#contentObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/train-kubernetes-docs/file/linux.rb', line 20

def content
  return @content if defined?(@content)

  @content = @backend.run_command("cat #{@spath} || echo -n",
                                  { pod: pod, namespace: namespace, container: container })
                     .stdout
  return @content unless @content.empty?

  @content = nil if directory? || size.nil? || (size > 0)
  @content
end

#content=(new_content) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/train-kubernetes-docs/file/linux.rb', line 32

def content=(new_content)
  execute_result = @backend.run_command('base64 --help', { pod: pod, namespace: namespace, container: container })
  if execute_result.exit_status != 0
    raise TransportError, "#{self.class} found no base64 binary for file writes"
  end

  unix_cmd = format("echo '%<base64>s' | base64 --decode > %<file>s",
                    base64: Base64.strict_encode64(new_content),
                    file: @spath)

  @backend.run_command(unix_cmd, { pod: pod, namespace: namespace, container: container })

  @content = new_content
end

#exist?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/train-kubernetes-docs/file/linux.rb', line 47

def exist?
  @exist ||= begin
    f = @follow_symlink ? '' : " || test -L #{@spath}"
    @backend.run_command("test -e #{@spath}" + f,
                         { pod: pod, namespace: namespace, container: container })
            .exit_status == 0
  end
end

#inherited?Boolean

Returns:

  • (Boolean)


124
125
126
127
128
# File 'lib/train-kubernetes-docs/file/linux.rb', line 124

def inherited?
  return false unless exist?

  skip_resource '`inherited?` is not supported on your Linux Containers yet.'
end

#mountedObject



56
57
58
59
60
# File 'lib/train-kubernetes-docs/file/linux.rb', line 56

def mounted
  @mounted ||=
    @backend.run_command("mount | grep -- ' on #{@path} '",
                         { pod: pod, namespace: namespace, container: container })
end

#pathObject



62
63
64
65
66
# File 'lib/train-kubernetes-docs/file/linux.rb', line 62

def path
  return @path unless @follow_symlink && symlink?

  @link_path ||= read_target_path
end


68
69
70
71
72
73
74
75
# File 'lib/train-kubernetes-docs/file/linux.rb', line 68

def shallow_link_path
  return nil unless symlink?

  @shallow_link_path ||=
    @backend.run_command("readlink #{@spath}", { pod: pod, namespace: namespace, container: container })
            .stdout
            .chomp
end

#sourceObject



110
111
112
113
114
115
116
# File 'lib/train-kubernetes-docs/file/linux.rb', line 110

def source
  if @follow_symlink
    self.class.new(@backend, @path, false, pod: pod, container: container, namespace: namespace)
  else
    self
  end
end

#statObject



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
104
105
106
107
108
# File 'lib/train-kubernetes-docs/file/linux.rb', line 77

def stat
  @stat ||= begin
    shell_escaped_path = @spath
    backend = @backend
    follow_symlink = @follow_symlink
    lstat = follow_symlink ? ' -L' : ''
    format = '--printf'
    res = backend.run_command("stat#{lstat} #{shell_escaped_path} 2>/dev/null #{format} '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'",
                              { pod: pod, namespace: namespace, container: container })
    # ignore the exit_code: it is != 0 if selinux labels are not supported
    # on the system.

    fields = res.stdout.split("\n")
    return {} if fields.length != 9

    tmask = fields[1].to_i(16)
    selinux = fields[8]
    ## selinux security context string not available on esxi
    selinux = nil if (selinux == '?') || (selinux == '(null)') || (selinux == 'C')
    {
      type: Train::Extras::Stat.find_type(tmask),
      mode: tmask & 07777,
      owner: fields[2],
      uid: fields[3].to_i,
      group: fields[4],
      gid: fields[5].to_i,
      mtime: fields[7].to_i,
      size: fields[0].to_i,
      selinux_label: selinux
    }
  end
end

#user_permissionsObject



118
119
120
121
122
# File 'lib/train-kubernetes-docs/file/linux.rb', line 118

def user_permissions
  return {} unless exist?

  skip_reource '`user_permissions` is not supported on your Linux Containers yet.'
end