Class: Yast::LxcClass

Inherits:
Module
  • Object
show all
Defined in:
../../src/modules/Lxc.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) CheckLXCConfiguration

Check if LXC is correctly configured



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File '../../src/modules/Lxc.rb', line 138

def CheckLXCConfiguration
  problem = false

  out = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), "/usr/bin/id --user")
  )
  root = Ops.get_string(out, "stdout", "") == "0\n"

  # zgrep does not seem to work with .target.bash_output -> grep uncompressed config
  tmpdir = Directory.tmpdir
  SCR.Execute(
    path(".target.bash_output"),
    Builtins.sformat(
      "cp /proc/config.gz '%1/' && gunzip '%1/config.gz'",
      String.Quote(tmpdir)
    )
  )

  out = Convert.to_map(
    SCR.Execute(
      path(".target.bash_output"),
      Builtins.sformat(
        "LANG=C GREP=grep CONFIG='%1/config' /usr/bin/lxc-checkconfig",
        String.Quote(tmpdir)
      )
    )
  )

  rt = []
  colors = { "blue" => "", "red" => "", "yellow" => "" }
  Builtins.foreach(
    Builtins.splitstring(Ops.get_string(out, "stdout", ""), "\n")
  ) do |line|
    colored = false
    Builtins.foreach(colors) do |name, color|
      raise Break if colored
      found2 = Builtins.find(line, color)
      if found2 != nil && Ops.greater_than(found2, 0)
        if @textmode
          line = Builtins.sformat(
            "%1<i>%2</i>",
            Builtins.substring(line, 0, found2),
            Builtins.substring(line, Ops.add(found2, Builtins.size(color)))
          )
        else
          line = Builtins.sformat(
            "%1<font color=%2>%3</font>",
            Builtins.substring(line, 0, found2),
            name,
            Builtins.substring(line, Ops.add(found2, Builtins.size(color)))
          )
        end
        colored = true
        if name == "red" || name == "yellow"
          # When running as root, "File capabilities" warning is not relevant (bnc#776172)
          if root && Builtins.issubstring(line, "File capabilities")
            Builtins.y2milestone(
              "File capabilities not met. Ignoring the warning for root user."
            )
          else
            problem = true
          end
        end
      end
    end
    # 'normalizing' color
    found = Builtins.find(line, "")
    if found != nil && Ops.greater_or_equal(found, 0)
      line = Ops.add(
        Builtins.substring(line, 0, found),
        Builtins.substring(line, Ops.add(found, Builtins.size("")))
      )
    end
    rt = Builtins.add(rt, line)
  end

  return true if !problem

  UI.OpenDialog(
    Opt(:decorated),
    HBox(
      HSpacing(1.5),
      VSpacing(30),
      VBox(
        HSpacing(85),
        VSpacing(0.5),
        # info label (try to keep the text short)
        Label(
          _(
            "Some problems with LXC configuration were found. Check the documentation for details."
          )
        ),
        VSpacing(0.5),
        # output follows in widget below
        Left(Label(_("Output of 'lxc-checkconfig' script:"))),
        RichText(Id(:rt), Builtins.mergestring(rt, "<br>")),
        PushButton("OK")
      )
    )
  )

  UI.UserInput
  UI.CloseDialog

  false
end

- (Object) CryptPassword(pw)

Encrypt given password using current method



81
82
83
84
85
86
87
# File '../../src/modules/Lxc.rb', line 81

def CryptPassword(pw)
  return Builtins.cryptmd5(pw) if @method == "md5"
  return Builtins.cryptblowfish(pw) if @method == "blowfish"
  return Builtins.cryptsha256(pw) if @method == "sha256"
  return Builtins.cryptsha512(pw) if @method == "sha512"
  Builtins.crypt(pw)
end

- (Object) GetContainers

Read list of containers and their states



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File '../../src/modules/Lxc.rb', line 110

def GetContainers
  ret = {}

  out = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), "lxc-ls -1")
  )

  Builtins.foreach(
    Builtins.sort(
      Builtins.splitstring(Ops.get_string(out, "stdout", ""), "\n")
    )
  ) do |line|
    if line != "" && !Builtins.haskey(ret, line)
      cmd = Builtins.sformat("lxc-info -n %1 | grep state", line)
      out = Convert.to_map(
        SCR.Execute(path(".target.bash_output"), cmd, { "LANG" => "C" })
      )
      state = Builtins.splitstring(
        Builtins.deletechars(Ops.get_string(out, "stdout", ""), " \t\n"),
        ":"
      )
      Ops.set(ret, line, Ops.get(state, 1, "") == "RUNNING")
    end
  end
  deep_copy(ret)
end

- (Object) main



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File '../../src/modules/Lxc.rb', line 35

def main
  Yast.import "UI"
  textdomain "lxc"

  Yast.import "Directory"
  Yast.import "FileUtils"
  Yast.import "Progress"
  Yast.import "Security"
  Yast.import "String"


  # text or graphic mode?
  @textmode = false

  # current password encryption method
  @method = "des"
end

- (Object) Read

Read all lxc settings

Returns:

  • true on success



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File '../../src/modules/Lxc.rb', line 247

def Read
  # Lxc read dialog caption
  caption = _("Initializing LXC Configuration")

  steps = 2

  # We do not set help text here, because it was set outside
  Progress.New(
    caption,
    " ",
    steps,
    [
      # Progress stage
      _("Check LXC availability"),
      # Progress stage
      _("Read system settings")
    ],
    [
      # Progress step
      _("Check LXC availability..."),
      # Progress step
      _("Reading system settings..."),
      # Progress finished
      _("Finished")
    ],
    ""
  )


  Progress.NextStage

  @textmode = Ops.get_boolean(UI.GetDisplayInfo, "TextMode", false)

  CheckLXCConfiguration()

  Progress.NextStage

  orig = Progress.set(false)

  Security.Read

  Progress.set(orig)

  security = Security.Export
  @method = Builtins.tolower(
    Ops.get_string(security, "PASSWD_ENCRYPTION", "des")
  )

  Progress.NextStage

  true
end

- (Object) ReadBridgesIds

Read list of available bridges



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File '../../src/modules/Lxc.rb', line 90

def ReadBridgesIds
  ret = []
  if FileUtils.Exists("/sbin/brctl")
    out = Convert.to_map(
      SCR.Execute(
        path(".target.bash_output"),
        "/sbin/brctl show | tail -n +2 | cut -f 1"
      )
    )

    if Ops.get_string(out, "stdout", "") != ""
      ret = Builtins.maplist(
        Builtins.splitstring(Ops.get_string(out, "stdout", ""), "\n")
      ) { |line| line }
    end
  end
  deep_copy(ret)
end

- (Object) ReadTemplates

read list of available templates



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
# File '../../src/modules/Lxc.rb', line 54

def ReadTemplates
  ret = []
  out = Convert.to_map(
    SCR.Execute(
      path(".target.bash_output"),
      "rpm -ql lxc | grep 'templates/' | cut -f 2- -d -"
    )
  )
  if Ops.get_string(out, "stdout", "") != ""
    Builtins.foreach(
      Builtins.sort(
        Builtins.splitstring(Ops.get_string(out, "stdout", ""), "\n")
      )
    ) do |line|
      if line != ""
        ret = Builtins.add(
          ret,
          # sles goes later = selected if present
          Item(line, line == "sles" || line == "opensuse")
        )
      end
    end
  end
  deep_copy(ret)
end