Skip to content

Commit

Permalink
Added proxy setup service
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Aug 16, 2023
1 parent 1f9547c commit 1d94bef
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion service/agama.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Gem::Specification.new do |spec|
spec.homepage = "https://github.com/openSUSE/agama"
spec.license = "GPL-2.0-only"
spec.files = Dir["lib/**/*.rb", "bin/*", "share/*", "etc/*"]
spec.executables = ["agamactl"]
spec.executables = ["agamactl", "agama-proxy-setup"]
spec.metadata = { "rubygems_mfa_required" => "true" }

spec.required_ruby_version = ">= 2.5.0"
Expand Down
111 changes: 111 additions & 0 deletions service/bin/agama-proxy-setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Copyright (c) [2023] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

# Helper script to create a configuration file for a selected list of products.

require "yast"
require "uri"

# This class is responsible of parsing the proxy url from the kernel cmdline or configured
# during the boot proccess of the system
class SetupProxy
include Singleton
include Yast
include Logger

CMDLINE_PATH = "/etc/cmdline"
CMDLINE_MENU_CONF = "/etc/cmdline-menu.conf"

attr_accessor :proxy

# Constructor
def initialize
Yast.import "Proxy"
end

def run
read
write
end

private

def read
self.proxy = proxy_from_cmdline || proxy_from_dracut
end

# TODO
def proxy_from_dracut
return unless File.exist?(CMDLINE_MENU_CONF)

options = File.read(CMDLINE_MENU_CONF)
proxy_url_from(options)
end

def proxy_url_from(options)
proxy_url = options.split.find { |o| o.start_with?(/proxy/i) }
return unless proxy_url

URI(proxy_url.downcase.gsub("proxy=", ""))
end

def proxy_from_cmdline
return unless File.exist?(CMDLINE_PATH)

options = File.read(CMDLINE_PATH)
proxy_url_from(options)
end

def proxy_import_settings
ex = Proxy.Export
proto = proxy.scheme

# save user name and password separately
ex["proxy_user"] = proxy.user
proxy.user = nil
ex["proxy_password"] = proxy.password
proxy.password = nil
ex["#{proto}_proxy"] = proxy.to_s
# Use the proxy also for https and ftp
if proto == "http"
ex["https_proxy"] = proxy.to_s
ex["ftp_proxy"] = proxy.to_s
end
ex["enabled"] = true
ex
end

def write
return unless proxy

Proxy.Read
ex = proxy_import_settings

log.info "Writing proxy settings: #{proxy.scheme}_proxy = '#{proxy}'"
log.debug "Writing proxy settings: #{ex}"

Proxy.Import(ex)
Proxy.Write
end
end

SetupProxy.instance.run
4 changes: 3 additions & 1 deletion service/package/gem2rpm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
install -D -m 0644 %{buildroot}%{gem_base}/gems/%{mod_full_name}/share/dbus.conf %{buildroot}%{_datadir}/dbus-1/agama.conf
install --directory %{buildroot}%{_datadir}/dbus-1/agama-services
install -m 0644 --target-directory=%{buildroot}%{_datadir}/dbus-1/agama-services %{buildroot}%{gem_base}/gems/%{mod_full_name}/share/org.opensuse.Agama*.service
install -D -m 0644 %{buildroot}%{gem_base}/gems/%{mod_full_name}/share/systemd.service %{buildroot}%{_unitdir}/agama.service
install -D -m 0644 %{buildroot}%{gem_base}/gems/%{mod_full_name}/share/agama.service %{buildroot}%{_unitdir}/agama.service
install -D -m 0644 %{buildroot}%{gem_base}/gems/%{mod_full_name}/share/agama-proxy-setup.service %{buildroot}%{_unitdir}/agama-proxy-setup.service
install -D -m 0644 %{buildroot}%{gem_base}/gems/%{mod_full_name}/etc/agama.yaml %{buildroot}%{_sysconfdir}/agama.yaml
:main:
:preamble: |-
Expand Down Expand Up @@ -36,4 +37,5 @@
%dir %{_datadir}/dbus-1/agama-services\n
%{_datadir}/dbus-1/agama-services/org.opensuse.Agama*.service\n
%{_unitdir}/agama.service\n
%{_unitdir}/agama-proxy-setup.service\n
%config %{_sysconfdir}/agama.yaml\n"
12 changes: 12 additions & 0 deletions service/share/agama-proxy-setup.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Configure wide proxy setup for agama and systemd services
Before=agama.service
Wants=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/bin/agama-proxy-setup

[Install]
WantedBy=default.target

File renamed without changes.

0 comments on commit 1d94bef

Please sign in to comment.