Skip to content

Commit

Permalink
Merge branch 'issue#932' of https://github.com/nagu165/agama into iss…
Browse files Browse the repository at this point in the history
…ue#932
  • Loading branch information
nagu165 committed Jan 17, 2024
2 parents b73a4a6 + 6b00245 commit a68be13
Show file tree
Hide file tree
Showing 19 changed files with 283 additions and 145 deletions.
53 changes: 4 additions & 49 deletions rust/agama-cli/doc/backend-for-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,8 @@ of [.github/workflows/ci.yml][ci.yml].

[ci.yml]: https://github.com/openSUSE/agama/blob/25462f57ab695d6910beb59ff0b21a7afaeda47e/.github/workflows/ci.yml

## Resulting Script

```sh
# https://build.opensuse.org/package/show/systemsmanagement:Agama:Staging/agama-testing
CIMAGE=registry.opensuse.org/systemsmanagement/agama/staging/containers/opensuse/agama-testing:latest
# rename this if you test multiple things
CNAME=agama
# the '?' here will report a shell error
# if you accidentally paste a command without setting the variable first
echo ${CNAME?}

test -f service/agama.gemspec || echo "You should run this from a checkout of agama"

# destroy the previous instance
podman stop ${CNAME?}
podman rm ${CNAME?}

mkdir -p ./mnt/log-yast2 # needed?
mkdir -p ./mnt/run-agama # only needed for D-Bus access from outside, unused now

# Update our image
podman pull ${CIMAGE?}

podman run --name ${CNAME?} \
--privileged --detach --ipc=host \
-v .:/checkout -v ./mnt/run-agama:/run/agama -v ./mnt/log-yast2:/var/log/YaST2 \
${CIMAGE?}

# shortcut for the following
CEXEC="podman exec ${CNAME?} bash -c"

${CEXEC?} "cd /checkout && ./setup-services.sh"

# Optional: explicit service start using a separate log file
${CEXEC?} "cd /checkout/service && (bundle exec bin/agamactl > service.log 2>&1 &)"

# Now the CLI is in the same repo, just symlink it
${CEXEC?} "ln -sfv /checkout/./rust/target/debug/agama /usr/bin/agama"

# Optional: Play!
${CEXEC?} "agama -f yaml config show"

# Optional: show logs of autostarted services
${CEXEC?} "journalctl --since=-5min"

# Optional: show logs of explicitly started services
${CEXEC?} "cat /checkout/service/service.log"

# Optional: Interactive shell in the container
podman exec --tty --interactive ${CNAME?} bash
```
The script, which used to be inlined here, is now at
[`/testing_using_container.sh`](../../../testing_using_container.sh).
>>>>>>> 8f2f0404 (copied the script part of rust/agama-cli/doc/backend-for-testing.md)
4 changes: 1 addition & 3 deletions rust/agama-dbus-server/src/l10n/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ pub fn init_locale() -> Result<LocaleCode, Box<dyn std::error::Error>> {
/// Sets the service locale.
///
pub fn set_service_locale(locale: &LocaleCode) {
// Let's force the encoding to be 'UTF-8'.
let locale = format!("{}.UTF-8", locale);
if setlocale(LocaleCategory::LcAll, locale).is_none() {
if setlocale(LocaleCategory::LcAll, locale.to_string()).is_none() {
log::warn!("Could not set the locale");
}
}
62 changes: 62 additions & 0 deletions rust/agama-dbus-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,57 @@ mod tests {
use crate::network::error::NetworkStateError;
use uuid::Uuid;

#[test]
fn test_macaddress() {
let mut val: Option<String> = None;
assert!(matches!(
MacAddress::try_from(&val).unwrap(),
MacAddress::Unset
));

val = Some(String::from(""));
assert!(matches!(
MacAddress::try_from(&val).unwrap(),
MacAddress::Unset
));

val = Some(String::from("preserve"));
assert!(matches!(
MacAddress::try_from(&val).unwrap(),
MacAddress::Preserve
));

val = Some(String::from("permanent"));
assert!(matches!(
MacAddress::try_from(&val).unwrap(),
MacAddress::Permanent
));

val = Some(String::from("random"));
assert!(matches!(
MacAddress::try_from(&val).unwrap(),
MacAddress::Random
));

val = Some(String::from("stable"));
assert!(matches!(
MacAddress::try_from(&val).unwrap(),
MacAddress::Stable
));

val = Some(String::from("This is not a MACAddr"));
assert!(matches!(
MacAddress::try_from(&val),
Err(InvalidMacAddress(_))
));

val = Some(String::from("de:ad:be:ef:2b:ad"));
assert_eq!(
MacAddress::try_from(&val).unwrap().to_string(),
String::from("de:ad:be:ef:2b:ad").to_uppercase()
);
}

#[test]
fn test_add_connection() {
let mut state = NetworkState::default();
Expand Down Expand Up @@ -457,6 +508,17 @@ impl FromStr for MacAddress {
}
}

impl TryFrom<&Option<String>> for MacAddress {
type Error = InvalidMacAddress;

fn try_from(value: &Option<String>) -> Result<Self, Self::Error> {
match &value {
Some(str) => MacAddress::from_str(str),
None => Ok(Self::Unset),
}
}
}

impl fmt::Display for MacAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let output = match &self {
Expand Down
20 changes: 17 additions & 3 deletions rust/agama-locale-data/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ pub struct LocaleCode {
pub language: String,
// ISO-3166
pub territory: String,
// encoding: String,
pub encoding: String,
}

impl Display for LocaleCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}_{}", &self.language, &self.territory)
write!(
f,
"{}_{}.{}",
&self.language, &self.territory, &self.encoding
)
}
}

Expand All @@ -25,6 +29,7 @@ impl Default for LocaleCode {
Self {
language: "en".to_string(),
territory: "US".to_string(),
encoding: "UTF-8".to_string(),
}
}
}
Expand All @@ -37,14 +42,23 @@ impl TryFrom<&str> for LocaleCode {
type Error = InvalidLocaleCode;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let locale_regexp: Regex = Regex::new(r"^([[:alpha:]]+)_([[:alpha:]]+)").unwrap();
let locale_regexp: Regex =
Regex::new(r"^([[:alpha:]]+)_([[:alpha:]]+)(?:\.(.+))?").unwrap();

let captures = locale_regexp
.captures(value)
.ok_or_else(|| InvalidLocaleCode(value.to_string()))?;

let encoding = captures
.get(3)
.map(|e| e.as_str())
.unwrap_or("UTF-8")
.to_string();

Ok(Self {
language: captures.get(1).unwrap().as_str().to_string(),
territory: captures.get(2).unwrap().as_str().to_string(),
encoding,
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions rust/package/agama-cli.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
-------------------------------------------------------------------
Thu Jan 11 15:34:15 UTC 2024 - Imobach Gonzalez Sosa <[email protected]>

- Include the encoding as part of the locales (gh#openSUSE/agama#987).

-------------------------------------------------------------------
Mon Jan 8 17:02:40 UTC 2024 - José Iván López González <[email protected]>

Expand Down
4 changes: 0 additions & 4 deletions rust/package/agama-cli.spec
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ License: GPL-2.0-only
Url: https://github.com/opensuse/agama
Source0: agama.tar
Source1: vendor.tar.zst
# Generated by the cargo_vendor OBS service
Source2: cargo_config
BuildRequires: cargo-packaging
BuildRequires: pkgconfig(openssl)
# used in tests for dbus service
Expand Down Expand Up @@ -64,8 +62,6 @@ DBus service for agama project. It provides so far localization service.

%prep
%autosetup -a1 -n agama
mkdir .cargo
cp %{SOURCE2} .cargo/config
# Remove exec bits to prevent an issue in fedora shebang checking. Uncomment only if required.
# find vendor -type f -name \*.rs -exec chmod -x '{}' \;

Expand Down
46 changes: 37 additions & 9 deletions service/lib/agama/dbus/software_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,8 @@ def start
# registering the language change callback to work properly
export
@ui_locale = UILocale.new(Clients::Locale.instance) do |locale|
# set the locale in the Language module, when changing the repository
# (product) it calls Pkg.SetTextLocale(Language.language) internally
Yast::Language.Set(locale)
# set libzypp locale (for communication only, Pkg.SetPackageLocale
# call can be used for installing the language packages)
Yast::Pkg.SetTextLocale(locale)
# TODO: libzypp shows the pattern names and descriptions using the
# locale set at the repository refresh time, here we should refresh
# the repositories with the new locale
# call the language change handler
locale_handler(locale)
end
end

Expand Down Expand Up @@ -101,6 +94,41 @@ def dbus_objects
Agama::DBus::Software::Proposal.new(logger)
]
end

# Language change callback handler, activate new locale in the libzypp backend
# @param locale [String] the new locale
def locale_handler(locale)
language, = locale.split(".")

# set the locale in the Language module, when changing the repository
# (product) it calls Pkg.SetTextLocale(Language.language) internally
Yast::Language.Set(language)

# set libzypp locale (for communication only, Pkg.SetPackageLocale
# call can be used for *installing* the language packages)
Yast::Pkg.SetTextLocale(language)

# refresh all enabled repositories to download the missing translation files
Yast::Pkg.SourceGetCurrent(true).each do |src|
Yast::Pkg.SourceForceRefreshNow(src)
end

# remember the currently selected packages and patterns by YaST
# (ignore the automatic selections done by the solver)
#
# NOTE: we will need to handle also the tabooed and soft-locked objects
# when we allow to set them via UI or CLI
selected = Y2Packager::Resolvable.find(status: :selected, transact_by: :appl_high)

# save and reload all repositories to activate the new translations
Yast::Pkg.SourceSaveAll
Yast::Pkg.SourceFinishAll
Yast::Pkg.SourceRestore
Yast::Pkg.SourceLoad

# restore back the selected objects
selected.each { |s| Yast::Pkg.ResolvableInstall(s.name, s.kind) }
end
end
end
end
8 changes: 8 additions & 0 deletions service/lib/agama/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def install
attr_reader :logger

ETC_NM_DIR = "/etc/NetworkManager"
RUN_NM_DIR = "/run/NetworkManager"
private_constant :ETC_NM_DIR

def enable_service
Expand All @@ -68,6 +69,13 @@ def enable_service
def copy_files
return unless Dir.exist?(ETC_NM_DIR)

# runtime configuration is copied first, so in case of later modification
# on same interface it gets overwriten (bsc#1210541).
copy_directory(
File.join(RUN_NM_DIR, "system-connections"),
File.join(Yast::Installation.destdir, ETC_NM_DIR, "system-connections")
)

copy_directory(
File.join(ETC_NM_DIR, "system-connections"),
File.join(Yast::Installation.destdir, ETC_NM_DIR, "system-connections")
Expand Down
8 changes: 4 additions & 4 deletions service/lib/agama/ui_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def initialize(locale_client, &block)
private

def change_locale(locale)
# TODO: check if we can use UTF-8 everywhere including strange arch consoles
Yast::WFM.SetLanguage(locale, "UTF-8")
language, encoding = locale.split(".")
Yast::WFM.SetLanguage(language, encoding)
# explicitly set ENV to get localization also from libraries like libstorage
ENV["LANG"] = locale + ".UTF-8"
log.info "set yast language to #{locale}"
ENV["LANG"] = locale
log.info "set yast locale to #{locale}"
# explicit call to textdomain to force fast gettext change of language ASAP
textdomain "installation"
end
Expand Down
16 changes: 16 additions & 0 deletions service/package/rubygem-agama.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
-------------------------------------------------------------------
Tue Jan 16 10:49:14 UTC 2024 - Michal Filka <[email protected]>

- bsc#1210541, gh#openSUSE/agama#516
- copy NM's runtime config created on dracut's request to the target
-------------------------------------------------------------------
Thu Jan 11 15:32:44 UTC 2024 - Imobach Gonzalez Sosa <[email protected]>

- Handle the encoding included in the UILocale D-Bus property
(gh#openSUSE/agama#987).

-------------------------------------------------------------------
Thu Jan 11 12:08:29 UTC 2024 - Ladislav Slezák <[email protected]>

- Translate the pattern descriptions (gh#openSUSE/agama#859)

-------------------------------------------------------------------
Thu Dec 21 14:23:48 UTC 2023 - Imobach Gonzalez Sosa <[email protected]>

Expand Down
3 changes: 2 additions & 1 deletion service/test/agama/dbus/manager_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
let(:users_obj) { instance_double(Agama::DBus::Users, path: "/org/opensuse/Agama/Users1") }

let(:locale_client) do
instance_double(Agama::DBus::Clients::Locale, ui_locale: "en_US", on_ui_locale_change: nil)
instance_double(Agama::DBus::Clients::Locale, ui_locale: "en_US.UTF-8",
on_ui_locale_change: nil)
end

before do
Expand Down
9 changes: 9 additions & 0 deletions setup-services.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ fi
# Rubygem dependencies
(
cd $MYDIR/service

if [ -d /checkout-ruby-dbus ]; then
# we are in a container, told to use that one
# instead of a released version
# edit +Gemfile and -gemspec
sed -e '/ruby-dbus/d' -i Gemfile agama.gemspec
sed -e '/gemspec/a gem "ruby-dbus", path: "/checkout-ruby-dbus"' -i Gemfile
fi

bundle config set --local path 'vendor/bundle'
bundle install
)
Expand Down
Loading

0 comments on commit a68be13

Please sign in to comment.