Class: Yast::SMTDataClass

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) AddNewCronJob(new_cron_job)

Adds new cron job.

Parameters:

  • new_cron_job (Hash)

See Also:

  • #GetCronSettings()


323
324
325
326
327
328
329
330
# File '../../src/modules/SMTData.rb', line 323

def AddNewCronJob(new_cron_job)
  new_cron_job = deep_copy(new_cron_job)
  Ops.set(new_cron_job, "user", @user_for_sync_script)

  @smt_cron_settings = Builtins.add(@smt_cron_settings, new_cron_job)

  nil
end

- (Object) AdjustSCCCredentialsFileAccess

Adjusts SCCcredentials file permissions to be accessible by SMT user defined in LOCAL->smtUser. By default it adjusts file ACL to “read”, or it changes the file owner and permissions to smt:root.



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

def AdjustSCCCredentialsFileAccess
  smt_user = GetSMTUser()

  if smt_user == nil || smt_user == ""
    Builtins.y2error("LOCAL->smtUser: %1", smt_user)
    return
  end

  cmd = Builtins.sformat(
    "/usr/bin/setfacl -m u:%1:r '%2'",
    smt_user,
    String.Quote(@SCCcredentials_file)
  )
  setfacl = Convert.to_integer(SCR.Execute(path(".target.bash"), cmd)) == 0
  Builtins.y2milestone("Adjusting file ACL (%1) returned %2", cmd, setfacl)

  # check whether the file is really accessible
  if CredentialsFileAccessible()
    if FileUtils.Exists("/usr/bin/getfacl")
      Builtins.y2milestone(
        "Credentials file is accessible now: %1",
        SCR.Execute(
          path(".target.bash_output"),
          Builtins.sformat(
            "/usr/bin/getfacl --access '%1' 2>/dev/null",
            String.Quote(@SCCcredentials_file)
          )
        )
      )
    else
      Builtins.y2milestone("Credentials file is accessible now")
    end
    return
  end

  cmd = Builtins.sformat(
    "/bin/chown %1:root '%2'; /bin/chmod ug+r '%2'",
    smt_user,
    String.Quote(@SCCcredentials_file)
  )
  chownchmod = Convert.to_integer(SCR.Execute(path(".target.bash"), cmd)) == 0
  Builtins.y2milestone(
    "Adjusting file access (%1) returned %2",
    cmd,
    chownchmod
  )

  nil
end

- (Object) AskForNewRootPassword



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
# File '../../src/modules/SMTData.rb', line 1071

def AskForNewRootPassword
  ret = nil

  UI.OpenDialog(
    VBox(
      VSpacing(1),
      Heading(_("Adjusting New Database root Password")),
      VSpacing(1),
      HBox(
        HSpacing(2),
        VBox(
          Label(
            _(
              "The current MariaDB root password is empty.\n" +
                "\n" +
                "                                          For security reasons, please, set a new one."
            )
          ),
          VSpacing(1),
          Password(Id(:new_root_password_1), _("New MariaDB root &Password")),
          Password(Id(:new_root_password_2), _("New Password &Again"))
        ),
        HSpacing(2)
      ),
      VSpacing(1),
      HBox(
        PushButton(Id(:ok), Opt(:default, :key_F10), Label.OKButton),
        HSpacing(2),
        PushButton(Id(:cancel), Opt(:key_F9), Label.CancelButton)
      ),
      VSpacing(1)
    )
  )

  UI.SetFocus(Id(:new_root_password_1))

  while true
    user_ret = UI.UserInput

    if user_ret == :cancel
      ret = nil
      break
    elsif user_ret == :ok
      pass_1 = Convert.to_string(
        UI.QueryWidget(Id(:new_root_password_1), :Value)
      )
      pass_2 = Convert.to_string(
        UI.QueryWidget(Id(:new_root_password_2), :Value)
      )

      if pass_1 == nil || pass_1 == ""
        UI.SetFocus(Id(:new_root_password_1))
        Report.Error(_("Set up a new password, please."))
        next
      elsif pass_1 != pass_2
        UI.SetFocus(Id(:new_root_password_2))
        Report.Error(_("The first and the second password do not match."))
        next
      end

      Builtins.y2milestone("New password provided")
      ret = pass_1
      break
    end
  end

  UI.CloseDialog

  ret
end

- (Object) AskForRootPassword(message)



965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File '../../src/modules/SMTData.rb', line 965

def AskForRootPassword(message)
  ret = nil

  UI.OpenDialog(
    VBox(
      VSpacing(1),
      Heading(_("Database root Password Required")),
      VSpacing(1),
      HBox(
        HSpacing(2),
        VBox(
          message != "" && message != nil ?
            VBox(Label(message), VSpacing(1)) :
            Empty(),
          Password(Id(:root_password), _("Enter the MariaDB root &Password"))
        ),
        HSpacing(2)
      ),
      VSpacing(1),
      HBox(
        PushButton(Id(:ok), Opt(:default, :key_F10), Label.OKButton),
        HSpacing(2),
        PushButton(Id(:cancel), Opt(:key_F9), Label.CancelButton)
      ),
      VSpacing(1)
    )
  )

  UI.SetFocus(Id(:root_password))

  user_ret = UI.UserInput
  if user_ret == :ok
    ret = Convert.to_string(UI.QueryWidget(Id(:root_password), :Value))
  end

  UI.CloseDialog

  ret
end

- (Object) ChangePasswordIfDifferent

Function changes the SMT-user passowrd if different than the initial one



1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
# File '../../src/modules/SMTData.rb', line 1328

def ChangePasswordIfDifferent
  # Keeping the service stopped
  if @smt_enabled_initial == false && !GetSMTServiceStatus()
    Builtins.y2milestone("SMT is disabled, not adjusting database...")
    return true
  end

  # bnc #389804
  # exit 5 from smt-db setup means that database exists and cpw
  # should be called
  if @database_already_exists != true
    Builtins.y2milestone(
      "Database and user were just created, skipping cpw ..."
    )
    return true
  end

  new_password = GetCredentials("DB", "pass")

  if @initial_password == new_password
    Builtins.y2milestone("SMT-user password is the same")
    return true
  end

  # /usr/lib/SMT/bin/smt-db cpw
  # * mysql root password
  # * smt user name
  # * smt user password
  # * verify smt user password
  smt_cpw_command = Builtins.sformat(
    "%1\n%2\n%3\n",
    @initial_password,
    GetCredentials("DB", "pass"),
    GetCredentials("DB", "pass")
  )

  smt_command_file = Builtins.sformat(
    "%1/smt-cpw-command-filename",
    Directory.tmpdir
  )
  SCR.Write(path(".target.string"), smt_command_file, "")
  # rw only for owner
  SCR.Execute(
    path(".target.bash"),
    Builtins.sformat("chmod 0600 '%1'", String.Quote(smt_command_file))
  )
  SCR.Write(path(".target.string"), smt_command_file, smt_cpw_command)

  Builtins.y2milestone("Calling smt-db cpw")
  retcode = Convert.to_integer(
    SCR.Execute(
      path(".target.bash"),
      Builtins.sformat(
        "/usr/lib/SMT/bin/smt-db cpw --yast < '%1'",
        smt_command_file
      )
    )
  )
  Builtins.y2milestone("smt-db setup returned: %1", retcode)

  if retcode != 0
    Report.Error(_("Cannot change SMT user password."))
    return false
  else
    return true
  end

  SCR.Execute(path(".target.remove"), smt_command_file)
end

- (Boolean) CheckAndAdjustApacheConfiguration

Function checks and adjusts the apache configuration to be usable by SMT.

Returns:

  • (Boolean)

    if successful



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# File '../../src/modules/SMTData.rb', line 574

def CheckAndAdjustApacheConfiguration
  apache_conf_file = "/etc/sysconfig/apache2"

  apache_conf_changed = false

  if !SCR.RegisterAgent(
      path(".http_server_conf"),
      term(:ag_ini, term(:SysConfigFile, apache_conf_file))
    )
    Builtins.y2error("Cannot register agent")
    Message.CannotWriteSettingsTo(apache_conf_file)
    return false
  end

  # "perl" in /etc/sysconfig/apache2:APACHE_MODULES
  ap_modules = Convert.to_string(
    SCR.Read(path(".http_server_conf.APACHE_MODULES"))
  )
  ap_modules_old = ap_modules

  ap_modules_l = Builtins.splitstring(ap_modules, " \t")
  ap_modules_l = Builtins.filter(ap_modules_l) do |one_ap_module|
    one_ap_module != nil && one_ap_module != ""
  end
  ap_modules_l = Builtins.toset(
    Convert.convert(
      Builtins.union(ap_modules_l, ["perl"]),
      :from => "list",
      :to   => "list <string>"
    )
  )
  ap_modules = Builtins.mergestring(ap_modules_l, " ")

  if ap_modules != ap_modules_old
    Builtins.y2milestone("Writing APACHE_MODULES")
    SCR.Write(path(".http_server_conf.APACHE_MODULES"), ap_modules)
    apache_conf_changed = true
  end

  # SSL in /etc/sysconfig/apache2:APACHE_SERVER_FLAGS
  ap_serflag = Convert.to_string(
    SCR.Read(path(".http_server_conf.APACHE_SERVER_FLAGS"))
  )
  ap_serflag_old = ap_serflag

  ap_serflag_l = Builtins.splitstring(ap_serflag, " \t")
  ap_serflag_l = Builtins.filter(ap_serflag_l) do |one_ap_serflag|
    one_ap_serflag != nil && one_ap_serflag != ""
  end
  ap_serflag_l = Builtins.toset(
    Convert.convert(
      Builtins.union(ap_serflag_l, ["SSL"]),
      :from => "list",
      :to   => "list <string>"
    )
  )
  ap_serflag = Builtins.mergestring(ap_serflag_l, " ")

  if ap_serflag != ap_serflag_old
    Builtins.y2milestone("Writing APACHE_SERVER_FLAGS")
    SCR.Write(path(".http_server_conf.APACHE_SERVER_FLAGS"), ap_serflag)
    apache_conf_changed = true
  end

  # Something has been changed
  # Apache conf needs writing
  # and Apache service needs restarting
  if apache_conf_changed
    Builtins.y2milestone("Writing sysconfig/apache conf")
    if !SCR.Write(path(".http_server_conf"), nil)
      Builtins.y2error("Cannot write apache2 conf")
      Message.CannotWriteSettingsTo(apache_conf_file)
      return false
    end
  end

  if !SCR.UnregisterAgent(path(".http_server_conf"))
    Builtins.y2error("Cannot unregister agent")
  end

  true
end

- (Boolean) CheckAndAdjustCredentialsFileAccess

Checks (and adjusts) the SCCcredentials file permissions the be readable by SMT user defined in LOCAL->smtUser. Returns if accessible and/or permissions successfuly set. If the file does not exist, nil is returned.

Returns:

  • (Boolean)

    if succesful (file exists and is accessible)



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

def CheckAndAdjustCredentialsFileAccess
  if SystemIsRegistered() != true
    Builtins.y2warning("Credentials file does not exist")
    return nil
  end

  state = CredentialsFileAccessible()
  Builtins.y2milestone(
    "Initial state: SCCcredentials are readable by SMT user: %1",
    state
  )

  if state == true
    return true
  elsif state == nil
    return false
  end

  AdjustSCCCredentialsFileAccess()

  state = CredentialsFileAccessible()
  Builtins.y2milestone(
    "Final state: SCCcredentials are readable by SMT user: %1",
    state
  )

  state
end

- (Object) CheckAndAdjustMirroredReposAccess

Returns whether the directory for mirroring repositories is writable by the SMT user (or whether it was possible to make it so).



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File '../../src/modules/SMTData.rb', line 226

def CheckAndAdjustMirroredReposAccess
  user = GetCredentials("DB", "user")
  user = "smt" if user == nil

  directory = GetMirroredReposDirectory()

  if Convert.to_boolean(
      SCR.Read(path(".smt.check_directory"), { "directory" => directory })
    ) == true
    return true
  end

  cmd = Builtins.sformat(
    "/bin/chown -R '%1:www' '%2' && /bin/chmod -R u+w '%2'",
    String.Quote(user),
    String.Quote(directory)
  )
  Builtins.y2milestone("Changing directory owner and permissions: %1", cmd)
  cmd_ret = Convert.to_map(SCR.Execute(path(".target.bash_output"), cmd))

  if Ops.get_integer(cmd_ret, "exit", -1) == 0
    Builtins.y2milestone("Successful: %1", cmd_ret)
    return true
  end

  Builtins.y2error(
    "Cannot change directory (%1) owner (%2) and permissions: %3",
    user,
    directory,
    cmd_ret
  )

  nil
end

- (Boolean) CredentialsFileAccessible

Returns whether a SCCcredentials file is accessible by SMT user defined in LOCAL->smtUser. Returns nil if the file does not exist.

Returns:

  • (Boolean)

    whether accessible



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File '../../src/modules/SMTData.rb', line 142

def CredentialsFileAccessible
  smt_user = GetSMTUser()
  return nil if smt_user == nil || smt_user == ""

  Convert.to_integer(
    SCR.Execute(
      path(".target.bash"),
      Builtins.sformat(
        "echo '' | /usr/bin/sudo -S -u '%1' /bin/cat '%2'",
        String.Quote(smt_user),
        String.Quote(@SCCcredentials_file)
      )
    )
  ) == 0
end

- (Object) CronRandomize



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File '../../src/modules/SMTData.rb', line 737

def CronRandomize
  return if !IsFirstRun()

  Builtins.y2milestone("First-run: randomizing cron times")

  # to get different 'random' numbers :->>>
  Builtins.srandom

  hour_smt_daily = 20
  minute_smt_daily = 0

  # randomize smt-daily
  counter = -1
  Builtins.foreach(GetCronSettings()) do |one_cron_job|
    counter = Ops.add(counter, 1)
    if Builtins.regexpmatch(
        Ops.get_string(one_cron_job, "command", ""),
        "smt-daily"
      )
      # 20:00 - 02:59
      hour_calc = Ops.subtract(Builtins.random(7), 4)
      if Ops.greater_or_equal(hour_calc, 0)
        hour_smt_daily = hour_calc
      else
        hour_smt_daily = Ops.add(24, hour_calc)
      end

      minute_smt_daily = Builtins.random(59)

      Ops.set(one_cron_job, "hour", Builtins.tostring(hour_smt_daily))
      Ops.set(one_cron_job, "minute", Builtins.tostring(minute_smt_daily))
      Builtins.y2milestone(
        "smt-daily randomized, hour: %1, minute: %2",
        Ops.get_string(one_cron_job, "hour", ""),
        Ops.get_string(one_cron_job, "minute", "")
      )
      ReplaceCronJob(counter, one_cron_job)
    end
  end

  # randomize smt-gen-report
  counter = -1
  Builtins.foreach(GetCronSettings()) do |one_cron_job|
    counter = Ops.add(counter, 1)
    if Builtins.regexpmatch(
        Ops.get_string(one_cron_job, "command", ""),
        "smt-gen-report"
      )
      # 4:00 - 6:59
      hour_report = Ops.add(Builtins.random(3), 4)
      Ops.set(one_cron_job, "hour", Builtins.tostring(hour_report))
      Ops.set(one_cron_job, "minute", Builtins.tostring(minute_smt_daily))
      Builtins.y2milestone(
        "smt-gen-report randomized, hour: %1, minute: %2",
        Ops.get_string(one_cron_job, "hour", ""),
        Ops.get_string(one_cron_job, "minute", "")
      )
      ReplaceCronJob(counter, one_cron_job)
    end
  end

  # randomize smt-repeated-register
  # 450 (seconds) == 7.5 minutes
  SetCredentials(
    "LOCAL",
    "rndRegister",
    Builtins.tostring(Builtins.random(450))
  )

  # take care the the post script do not reschedule again
  SCR.Execute(
    path(".target.bash"),
    "touch /var/lib/smt/RESCHEDULE_SYNC_DONE"
  )

  nil
end

- (String) GetCredentials(location, entry_name)

Returns value for credentials entry.

Examples:

GetCredentials ("LOCAL", "MirrorTo") -> "/srv/www/htdocs"

Parameters:

  • location (String)
  • entry_name (String)

Returns:

  • (String)

    value

See Also:



466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File '../../src/modules/SMTData.rb', line 466

def GetCredentials(location, entry_name)
  if !Builtins.haskey(@all_credentials, location)
    Builtins.y2warning("Key %1 not defined", location)
    return ""
  elsif !Builtins.haskey(
      Ops.get(@all_credentials, location, {}),
      entry_name
    )
    Builtins.y2warning("Key %1/%2 not defined", location, entry_name)
    return ""
  end

  Ops.get(@all_credentials, [location, entry_name], "")
end

- (Boolean) GetCredentialsDefined(location, entry_name)

Returns whether pair location/entry_name is known.

Examples:

GetCredentialsDefined ("LOCAL", "MirrorTo") -> true
GetCredentialsDefined ("LOCAL", "Unknown") -> false

Parameters:

  • location (String)
  • entry_name (String)

Returns:

  • (Boolean)

    if known



490
491
492
493
# File '../../src/modules/SMTData.rb', line 490

def GetCredentialsDefined(location, entry_name)
  Builtins.haskey(@all_credentials, location) &&
    Builtins.haskey(Ops.get(@all_credentials, location, {}), entry_name)
end

- (Array<Hash>) GetCronSettings

Returns list of scheduled NU mirrorings.

Structure:

[
   $[
     "command":"...",
     "day_of_month":"...",
     "day_of_week":"...",
     "hour":"...",
     "minute":"...",
     "month":"...",
     "user":"...",
   ],
   ...
 ]

Returns:

  • (Array<Hash>)


314
315
316
# File '../../src/modules/SMTData.rb', line 314

def GetCronSettings
  deep_copy(@smt_cron_settings)
end

- (String) GetMirroredReposDirectory

Returns directory where the remote repositories are mirrored to.

Returns:

  • (String)

    directory



215
216
217
218
219
220
# File '../../src/modules/SMTData.rb', line 215

def GetMirroredReposDirectory
  directory = GetCredentials("LOCAL", "MirrorTo")
  directory = "/srv/www/htdocs" if directory == nil

  Builtins.sformat("%1/repo/", directory)
end

- (Object) GetMysqlHostname



1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
# File '../../src/modules/SMTData.rb', line 1142

def GetMysqlHostname
  hostname = "localhost"

  db_config = GetCredentials("DB", "config")
  Builtins.foreach(Builtins.splitstring(db_config, ";")) do |one_db_item|
    if Builtins.regexpmatch(one_db_item, ".*host=.*")
      hostname = Builtins.regexpsub(one_db_item, ".*host=(.*)", "\\1")
      Builtins.y2milestone("Hostname found: %1", hostname)
      raise Break
    end
  end

  hostname
end

- (Object) GetSCCcredentialsFile



83
84
85
# File '../../src/modules/SMTData.rb', line 83

def GetSCCcredentialsFile
  @SCCcredentials_file
end

- (Object) GetSMTServiceStatus



424
425
426
# File '../../src/modules/SMTData.rb', line 424

def GetSMTServiceStatus
  @smt_enabled
end

- (Object) GetSMTUser

Returns SMT user (LOCAL->smtUser) or nil if not set or erroneous



96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
136
# File '../../src/modules/SMTData.rb', line 96

def GetSMTUser
  return @_smt_user if @_smt_user_loaded == true

  @_smt_user_loaded = true

  @_smt_user = GetCredentials("LOCAL", "smtUser")
  if @_smt_user == nil || @_smt_user == ""
    # The default user
    @_smt_user = "smt"
  end


  user_exists = Convert.to_integer(
    SCR.Execute(
      path(".target.bash"),
      Builtins.sformat(
        "/usr/bin/getent passwd '%1'",
        String.Quote(@_smt_user)
      )
    )
  )

  if user_exists == 0
    Builtins.y2milestone("User %1 exists", @_smt_user)
  else
    Builtins.y2error("User %1 doesn't exist!", @_smt_user)
    # Pop-up error message, %1 is replaced with the SMT user name,
    # %2 with the SMT config file
    Report.Error(
      Builtins.sformat(
        _(
          "SMT configuration is broken.\nLOCAL->smtUser %1 defined in %2 does not exist on the system."
        ),
        @_smt_user,
        @smt_conf
      )
    )
  end

  @_smt_user
end

- (Boolean) HandleMissingCACert

Handles a missing CA certificate. Returns boolean value whether CA checking should be finished.

Returns:

  • (Boolean)

    whether to finish the CA checking



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File '../../src/modules/SMTData.rb', line 860

def HandleMissingCACert
  Builtins.y2milestone("CA file %1 doesn't exist", @server_cert)
  ca_mgm_package = "yast2-ca-management"

  if Popup.AnyQuestion(
      # TRANSLATORS: Pop-up question headline
      _("Missing Server Certificate"),
      # TRANSLATORS: Pop-up question, %1 is replaced with a file name
      Builtins.sformat(
        _(
          "Server certificate %1 does not exist.\n" +
            "Would you like to run CA management to create one?\n" +
            "\n" +
            "The server certificate is vitally important for the update server to support SSL.\n"
        ),
        @server_cert
      ),
      _("&Run CA management"),
      _("&Skip"),
      @ca_already_called ? :focus_no : :focus_yes
    )
    # Package needs to be installed
    if PackageSystem.CheckAndInstallPackagesInteractive([ca_mgm_package])
      Builtins.y2milestone("Running ca_mgm")
      @ca_already_called = true

      Wizard.OpenNextBackDialog
      progress_orig = Progress.set(false)

      # bnc #400782
      # Custom control file can make ProductControl not to find anything
      previous_custom_control_file = ProductControl.custom_control_file
      if previous_custom_control_file != ""
        Builtins.y2milestone(
          "Previous custom_control_file: %1",
          previous_custom_control_file
        )
      end

      # bnc #471162
      # Use own control file (do not depend on the system one which might be missing)
      ProductControl.custom_control_file = @smt_control_file
      ProductControl.Init

      # Call installation proposal (service: CA)
      ret = WFM.call("inst_proposal", [{ "proposal" => "smt_ca" }])

      Builtins.y2milestone(
        "Restoring previous custom_control_file: %1",
        previous_custom_control_file
      )
      ProductControl.custom_control_file = previous_custom_control_file
      ProductControl.Init

      Builtins.y2milestone("Service proposal returned: %1", ret)
      if ret == :next
        ret = WFM.call("ca_mgm_proposal", ["Write"])
        Builtins.y2milestone("Storing CA management settings returned: %1", ret)
      end
      Progress.set(progress_orig)
      Wizard.CloseDialog

      return false 
      # Package is not installed and couldn't be installed
    else
      Report.Error(
        Builtins.sformat(
          _("Cannot run CA management because package %1 is not installed."),
          ca_mgm_package
        )
      )
      return true
    end
  else
    Builtins.y2warning("User doesn't want to run CA management.")
    return true
  end
end

- (Boolean) HandleSMTDBSetupRetcode(retcode_was)

Returns whether to try again

Returns:

  • (Boolean)

    whether to try again



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
# File '../../src/modules/SMTData.rb', line 1006

def HandleSMTDBSetupRetcode(retcode_was)
  # 2 : Cannot read the SMT configuration file
  if retcode_was == 2
    return Report.AnyQuestion(
      Builtins.sformat(_("Unable to read %1"), "/etc/smt.conf"),
      _(
        "SMT was not able to read the configuration file.\n" +
          "Please, check the file and click Retry.\n" +
          "\n" +
          "To skip this, click Skip.\n"
      ),
      _("&Retry"),
      _("&Skip"),
      :yes_button
    ) 

    # 4 : Cannot connect to database (wrong mysql root password?)
  elsif retcode_was == 4
    @mysql_root_password = AskForRootPassword(
      _(
        "SMT was not able to connect to the database.\nThe root password was probably wrong.\n"
      )
    )
    if @mysql_root_password != nil
      return true
    else
      return false
    end 

    # 5 : SMT database already exists
  elsif retcode_was == 5
    @database_already_exists = true
    Builtins.y2milestone("SMT database already exists")
    return false 

    # 7 : Cannot create Database smt
  elsif retcode_was == 7
    return Report.AnyQuestion(
      _("Cannot create database"),
      _(
        "SMT was not able to create the database.\nClick Retry to try again.\n"
      ),
      _("&Retry"),
      _("&Skip"),
      :yes_button
    ) 

    # 21: Database migration failed
  elsif retcode_was == 21
    return Report.AnyQuestion(
      _("Database migration failed"),
      _(
        "SMT has failed to migrate the database.\nClick Retry to try again.\n"
      ),
      _("&Retry"),
      _("&Skip"),
      :yes_button
    )
  else
    Builtins.y2warning("Return code %1 not handled", retcode_was)
  end

  false
end

- (Object) InitialConfig



91
92
93
# File '../../src/modules/SMTData.rb', line 91

def InitialConfig
  @initial_config
end

- (Object) IsFirstRun

Returns whether the current YaST run is the first run of the SMT configuration.



394
395
396
# File '../../src/modules/SMTData.rb', line 394

def IsFirstRun
  @first_run
end

- (Object) main



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
79
80
81
# File '../../src/modules/SMTData.rb', line 13

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

  Yast.import "Message"
  Yast.import "Service"
  Yast.import "Report"
  Yast.import "FileUtils"
  Yast.import "String"
  Yast.import "Label"
  Yast.import "Popup"
  Yast.import "PackageSystem"
  Yast.import "Wizard"
  Yast.import "Progress"
  Yast.import "Directory"
  Yast.import "ProductControl"

  @all_credentials = {}

  @smt_conf = "/etc/smt.conf"

  # script entered into a crontab
  @cron_mirror_script = "/usr/bin/smt-mirror.pl"
  # cron
  @path_to_cron = "/etc/smt.d/novell.com-smt"

  @path_to_scc_sync_script = "/usr/sbin/smt-scc-sync"
  @user_for_sync_script = "root"

  @server_cert = "/etc/pki/trust/anchors/YaST-CA.pem"
  @apache_cert = "/srv/www/htdocs/smt.crt"

  # own control file
  @smt_control_file = "/usr/share/YaST2/control/smt_control.xml"

  @smt_cron_settings = []

  @first_run = nil

  @first_run_file = Builtins.sformat(
    "%1/smt-already-adjusted",
    Directory.vardir
  )

  @smt_enabled = nil

  @smt_services = ["smt.target"]

  @smt_database = "mysql"

  @SCCcredentials_file = "/etc/zypp/credentials.d/SCCcredentials"
  @NCCcredentials_file = "/etc/zypp/credentials.d/NCCcredentials"

  @initial_config = false

  @_smt_user = nil
  @_smt_user_loaded = false

  @smt_enabled_initial = nil

  @ca_already_called = false

  @mysql_root_password = ""

  # bnc #389804
  @database_already_exists = false

  @initial_password = nil
end

- (Boolean) ReadCredentials

Reads the current SMT credentials into the memory

Returns:

  • (Boolean)

    if successful



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File '../../src/modules/SMTData.rb', line 518

def ReadCredentials
  @all_credentials = {}

  Ops.set(@all_credentials, "NU", {})

  Builtins.foreach(SCR.Dir(path(".smt_conf.section"))) do |section|
    section_path = Builtins.add(path(".smt_conf.value"), section)
    Builtins.foreach(SCR.Dir(section_path)) do |one_entry|
      SetCredentials(
        section,
        one_entry,
        Convert.to_string(SCR.Read(Builtins.add(section_path, one_entry)))
      )
    end
  end

  if Ops.get(@all_credentials, ["NU", "NUUser"], "MIRRORUSER") == ""
    @initial_config = true
  end

  Builtins.y2milestone("Initial configuration: %1", @initial_config)
  true
end

- (Object) ReadCronSettings

Reads SMT cron settings and fills up internal data variables.



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File '../../src/modules/SMTData.rb', line 713

def ReadCronSettings
  if !FileUtils.Exists(@path_to_cron)
    Builtins.y2milestone("Creating file %1", @path_to_cron)
    cmd = Convert.to_map(
      SCR.Execute(
        path(".target.bash_output"),
        Builtins.sformat("touch '%1'", String.Quote(@path_to_cron))
      )
    )

    if Ops.get_integer(cmd, "exit", -1) != 0
      Builtins.y2error("Cannot create file %1: %2", @path_to_cron, cmd)
    end
  end

  @smt_cron_settings = Convert.convert(
    SCR.Read(path(".smt_cron")),
    :from => "any",
    :to   => "list <map>"
  )

  nil
end

- (Object) ReadFirstRun

Reads whether the configuration process has been already done.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File '../../src/modules/SMTData.rb', line 369

def ReadFirstRun
  if !FileUtils.Exists(@first_run_file)
    Builtins.y2milestone(
      "File %1 doesn't exist -> this is a first-run",
      @first_run_file
    )
    @first_run = true
  else
    Builtins.y2milestone("Not a first-time run...")
    @first_run = false
  end

  nil
end

- (Object) ReadSMTServiceStatus



398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File '../../src/modules/SMTData.rb', line 398

def ReadSMTServiceStatus
  @smt_enabled = false
  @smt_enabled_initial = false

  if IsFirstRun() == nil
    Builtins.y2error("Cannot evaluate first_run!")
  elsif IsFirstRun() == true
    @smt_enabled_initial = true
    @smt_enabled = true
    return
  end

  # default (changed later to false if something doesn't work)
  @smt_enabled = true
  @smt_enabled_initial = true

  Builtins.foreach(@smt_services) do |one_service|
    if !Service.Enabled(one_service)
      @smt_enabled = false
      @smt_enabled_initial = false
    end
  end

  nil
end

- (Object) RemoveCronJob(cron_job_id)

Removes a current cron job.

Parameters:

  • cron_job_id (Fixnum)

    (offset)

See Also:

  • #GetCronSettings()


357
358
359
360
361
362
363
364
365
366
# File '../../src/modules/SMTData.rb', line 357

def RemoveCronJob(cron_job_id)
  if cron_job_id == nil || Ops.less_than(cron_job_id, 0)
    Builtins.y2error("Undefined offset: %1", cron_job_id)
    return
  end

  @smt_cron_settings = Builtins.remove(@smt_cron_settings, cron_job_id)

  nil
end

- (Object) ReplaceCronJob(cron_job_id, new_settings)

Replaces the current cron job settings with new ones.

Parameters:

  • cron_job_id (Fixnum)

    (offset)

  • new_settings (Hash)

See Also:

  • #GetCronSettings()


338
339
340
341
342
343
344
345
346
347
348
349
350
# File '../../src/modules/SMTData.rb', line 338

def ReplaceCronJob(cron_job_id, new_settings)
  new_settings = deep_copy(new_settings)
  if cron_job_id == nil || Ops.less_than(cron_job_id, 0)
    Builtins.y2error("Undefined offset: %1", cron_job_id)
    return
  end

  Ops.set(new_settings, "user", @user_for_sync_script)

  Ops.set(@smt_cron_settings, cron_job_id, new_settings)

  nil
end

- (Object) RunSmallSync



657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File '../../src/modules/SMTData.rb', line 657

def RunSmallSync
  path_to_sync_script = @path_to_scc_sync_script
  if not GetCredentials("NU", "ApiType") == "SCC"
    Builtins.y2error("Synchronization from SCC not possible")
  end
  if !FileUtils.Exists(path_to_sync_script)
    Builtins.y2error("Sync script doesn't exist")
    Report.Error(
      Builtins.sformat(
        _(
          "Synchronization script %1 does not exist.\n" +
            "\n" +
            "Please, check your SMT installation."
        ),
        path_to_sync_script
      )
    )
    return false
  end

  Builtins.y2milestone("Running sync: %1", path_to_sync_script)

  cmd_out = Convert.to_map(
    SCR.Execute(path(".target.bash_output"), path_to_sync_script)
  )
  if Ops.get_integer(cmd_out, "exit", -1) != 0
    UI.OpenDialog(
      HBox(
        VSpacing(19),
        VBox(
          HSpacing(70),
          Left(Label(_("Running the synchronization script failed."))),
          RichText(
            Opt(:hstretch, :vstretch),
            Builtins.sformat(
              _(
                "<p><b><big>Details:</big></b></p>\n" +
                  "                                         <p><b>stdout:</b><br><pre>%1</pre></p>\n" +
                  "                                         <p><b>stderr:</b><br><pre>%2</pre></p>"
              ),
              Ops.get_string(cmd_out, "stdout", ""),
              Ops.get_string(cmd_out, "stderr", "")
            )
          ),
          PushButton(Id(:ok), Opt(:default, :key_F10), Label.OKButton)
        )
      )
    )
    UI.UserInput
    UI.CloseDialog
  end

  true
end

- (Object) SetCredentials(location, entry_name, value)

Sets name/value credential pairs.

Examples:

SetCredentials ("LOCAL", "MirrorTo", "/srv/www/htdocs")

Parameters:

  • location (String)
  • entry_name (String)
  • value (String)

See Also:



505
506
507
508
509
510
511
512
513
# File '../../src/modules/SMTData.rb', line 505

def SetCredentials(location, entry_name, value)
  if !Builtins.haskey(@all_credentials, location)
    Ops.set(@all_credentials, location, {})
  end

  Ops.set(@all_credentials, [location, entry_name], value)

  nil
end

- (Object) SetNewRootPassword(new_mysql_root_password)



1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
# File '../../src/modules/SMTData.rb', line 1157

def SetNewRootPassword(new_mysql_root_password)
  Builtins.y2milestone("Adjusting new root password")

  mysql_command = Builtins.sformat(
    "SET PASSWORD FOR root@%1=PASSWORD('%2');",
    GetMysqlHostname(),
    String.Quote(new_mysql_root_password)
  )

  mysql_command_file = Builtins.sformat(
    "%1/smt-rootpass-filename",
    Directory.tmpdir
  )
  SCR.Write(path(".target.string"), mysql_command_file, "")
  # rw only for owner
  SCR.Execute(
    path(".target.bash"),
    Builtins.sformat("chmod 0600 '%1'", String.Quote(mysql_command_file))
  )
  SCR.Write(path(".target.string"), mysql_command_file, mysql_command)

  cmd_cmd = Builtins.sformat(
    "mysql -u root -h %1 < '%2'",
    GetMysqlHostname(),
    mysql_command_file
  )
  cmd = Convert.to_map(SCR.Execute(path(".target.bash_output"), cmd_cmd))
  if Ops.get_integer(cmd, "exit", -1) != 0
    Builtins.y2error("Cannot set new root password: %1", cmd)
    Report.Error(_("Setting up new MariaDB root password failed."))
    return false
  end

  Builtins.y2milestone("Successful %1", cmd)
  true
end

- (Object) SetSMTServiceStatus(new_status)



428
429
430
431
432
433
434
435
436
437
# File '../../src/modules/SMTData.rb', line 428

def SetSMTServiceStatus(new_status)
  if new_status == nil
    Builtins.y2error("Cannot set 'nil' status!")
    return
  end

  @smt_enabled = new_status

  nil
end

- (Object) StartDatabaseIfNeeded



1194
1195
1196
1197
1198
1199
1200
1201
# File '../../src/modules/SMTData.rb', line 1194

def StartDatabaseIfNeeded
  if !Service.Start(@smt_database)
    Builtins.y2error("Cannot start database")
    return false
  end

  true
end

- (Object) StorePasswordTMP

Function remembers initial SMT-user password



1316
1317
1318
1319
1320
1321
1322
1323
1324
# File '../../src/modules/SMTData.rb', line 1316

def StorePasswordTMP
  if @initial_password == nil
    @initial_password = GetCredentials("DB", "pass")
  else
    Builtins.y2error("Initial password already set!")
  end

  nil
end

- (Object) SystemIsRegistered



87
88
89
# File '../../src/modules/SMTData.rb', line 87

def SystemIsRegistered
  FileUtils.Exists(@SCCcredentials_file) || FileUtils.Exists(@NCCCredentials_file)
end

- (Boolean) WriteCASettings

Function checks whether a server certificate exists and copies it to the apache directory. If a server certificate doesn't exist, user is offered to run the CA management.

Returns:

  • (Boolean)

    if successful



945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# File '../../src/modules/SMTData.rb', line 945

def WriteCASettings
  ret = false

  # check the existence of file and offer to run CA managament
  # if it doesn't then check the existence again... and again...
  while true
    # Server certificate is missing
    if !FileUtils.Exists(@server_cert)
      break if HandleMissingCACert() 

      # Server certificate exists
    else
      ret = true
      break
    end
  end

  ret
end

- (Boolean) WriteCredentials

Writes the current credentials to the SMT configuration file.

Returns:

  • (Boolean)

    if successful



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File '../../src/modules/SMTData.rb', line 545

def WriteCredentials
  failed = false

  Builtins.foreach(@all_credentials) do |base_key, current_credentials|
    Builtins.foreach(current_credentials) do |key, value|
      if !SCR.Write(
          Builtins.add(Builtins.add(path(".smt_conf.value"), base_key), key),
          value
        )
        Builtins.y2error("Cannot write to smt.conf/%1/%2", base_key, key)
        failed = true
      end
    end
  end

  if failed == false
    if !SCR.Write(path(".smt_conf"), nil)
      Builtins.y2error("Cannot write to smt.conf")
      failed = true
    end
  end

  failed == false
end

- (Object) WriteCronSettings



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
# File '../../src/modules/SMTData.rb', line 815

def WriteCronSettings
  service_name = "cron"

  if @smt_cron_settings == nil || Builtins.size(@smt_cron_settings) == 0
    if FileUtils.Exists(@path_to_cron) &&
        !Convert.to_boolean(
          SCR.Execute(path(".target.remove"), @path_to_cron)
        )
      Builtins.y2error("Cannot remove %1 cron-file", @path_to_cron)
    else
      Builtins.y2milestone("No cron settings at all")
      return true
    end
  end

  if !SCR.Write(path(".smt_cron"), @smt_cron_settings)
    Builtins.y2error("Writing cron failed")
    Report.Error(_("Cannot write cron settings."))
    return false
  end

  if Ops.greater_than(Builtins.size(@smt_cron_settings), 0)
    if !Service.Enabled(service_name)
      Service.Enable(service_name)
    else
      Builtins.y2milestone("Service cron already enabled, skipping ...")
    end
  end

  if !Service.Restart(service_name)
    Builtins.y2error("Reloading %1 failed", service_name)
    # TRANSLATORS: error message, %1 is replaced with a service-name
    Report.Error(
      Builtins.sformat(_("Reloading service %1 has failed."), service_name)
    )
    return false
  end

  true
end

- (Object) WriteDatabaseSettings



1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
# File '../../src/modules/SMTData.rb', line 1203

def WriteDatabaseSettings
  # Keeping the service stopped
  if @smt_enabled_initial == false && !GetSMTServiceStatus()
    Builtins.y2milestone("SMT is disabled, not adjusting database...")
    return true
  end

  # check whether mysql root password is empty
  root_password_empty = Convert.to_integer(
    SCR.Execute(
      path(".target.bash"),
      Builtins.sformat(
        "echo 'show databases;' | mysql -u root -h %1 2>/dev/null",
        GetMysqlHostname()
      )
    )
  ) == 0

  # root password is empty
  # ask user to set a new one
  if root_password_empty
    # exception: SMT user is root
    if GetSMTUser() == "root"
      Builtins.y2warning("SMT user is root, using root's new password")
      @mysql_root_password = GetCredentials("DB", "pass")
    else
      Builtins.y2warning("mysql root password is empty")
      @mysql_root_password = AskForNewRootPassword()

      if @mysql_root_password == nil
        Builtins.y2error("New password not provided")
        return false
      end
    end

    if !SetNewRootPassword(@mysql_root_password)
      Builtins.y2warning("Unable to set new root password, using empty one")
      @mysql_root_password = ""
    end 
    # root password has been already set
  else
    Builtins.y2milestone(
      "mysql root password is not empty, asking for it..."
    )
    @mysql_root_password = AskForRootPassword(
      _("SMT needs to set up the database.")
    )
    if @mysql_root_password == nil
      Builtins.y2error("Mysql-root password not provided")
      return false
    end
  end

  smt_command_file = Builtins.sformat(
    "%1/smt-command-filename",
    Directory.tmpdir
  )
  SCR.Write(path(".target.string"), smt_command_file, "")
  # rw only for owner
  SCR.Execute(
    path(".target.bash"),
    Builtins.sformat("chmod 0600 '%1'", String.Quote(smt_command_file))
  )

  ret = false

  while true
    # /usr/lib/SMT/bin/smt-db setup --yast
    # * mysql root password
    # * smt user name
    # * smt user password
    # * verify smt user password
    smt_db_command = Builtins.sformat(
      "%1\n%2\n%3\n%4\n",
      @mysql_root_password,
      GetCredentials("DB", "user"),
      GetCredentials("DB", "pass"),
      GetCredentials("DB", "pass")
    )

    # always write it again, bnc 387414
    SCR.Write(path(".target.string"), smt_command_file, smt_db_command)

    Builtins.y2milestone("Calling smt-db setup")
    retcode = Convert.to_integer(
      SCR.Execute(
        path(".target.bash"),
        Builtins.sformat(
          "/usr/lib/SMT/bin/smt-db setup --yast < '%1'",
          smt_command_file
        )
      )
    )
    Builtins.y2milestone("smt-db setup returned: %1", retcode)

    if retcode == 0
      Builtins.y2milestone("Success")
      ret = true
      break
    else
      if !HandleSMTDBSetupRetcode(retcode)
        Builtins.y2milestone("Finishing SMT-DB setup")
        break
      end
    end
  end

  SCR.Execute(path(".target.remove"), smt_command_file)

  ret
end

- (Object) WriteFirstRunStatus

Writes that the configuration has been already done.



385
386
387
388
389
390
391
# File '../../src/modules/SMTData.rb', line 385

def WriteFirstRunStatus
  if !SCR.Write(path(".target.ycp"), String.Quote(@first_run_file), [])
    Builtins.y2error("Cannot create file %1", @first_run_file)
  end

  nil
end

- (Object) WriteSMTServiceStatus



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File '../../src/modules/SMTData.rb', line 439

def WriteSMTServiceStatus
  if GetSMTServiceStatus() == true
    Builtins.foreach(@smt_services) do |one_service|
      # dependencies are now handled by systemd
      Service.Enable(one_service)
      Service.Start(one_service)
    end
  else
    Builtins.foreach(@smt_services) do |one_service|
      Service.Stop(one_service)
      Service.Disable(one_service)
    end
  end

  true
end