Skip to content

Commit

Permalink
Merge c55129e into f5be694
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Dec 18, 2018
2 parents f5be694 + c55129e commit 1fe6dcd
Show file tree
Hide file tree
Showing 19 changed files with 442 additions and 194 deletions.
3 changes: 3 additions & 0 deletions src/clients/configuration_management.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "y2configuration_management/clients/main"

Y2ConfigurationManagement::Clients::Main.new.run
3 changes: 0 additions & 3 deletions src/clients/configuration_management_formula_ng.rb

This file was deleted.

4 changes: 2 additions & 2 deletions src/desktop/configuration_management.desktop
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Desktop Entry]
Type=Application
Categories=Settings;System;Qt;X-SuSE-YaST;X-SuSE-YaST-Misc;
X-SuSE-YaST-Call=configuration_management_formula_ng
X-SuSE-YaST-Call=configuration_management

X-SuSE-YaST-Group=Misc
X-SuSE-YaST-AutoInst=all
Expand All @@ -13,6 +13,6 @@ X-SuSE-YaST-AutoInstClient=configuration_management_auto
X-SuSE-YaST-Keywords=salt,privisioning,configuration_management

Icon=yast2-software
Exec=/usr/bin/xdg-su -c '/sbin/yast2 configuration_management_formula_ng'
Exec=/usr/bin/xdg-su -c "/sbin/yast2 configuration_management"

Name=YaST2 Configuration Management
48 changes: 48 additions & 0 deletions src/lib/configuration_management/configurations/salt.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "configuration_management/configurations/base"
require "pathname"

module Yast
module ConfigurationManagement
Expand All @@ -13,6 +14,8 @@ class Salt < Base
attr_reader :states_url
# @return [URI,nil] Location of Salt pillars
attr_reader :pillar_url
# @return [Array<String>] States (including formulas) which will be applied
attr_reader :enabled_states

# Custom initialization code
#
Expand All @@ -21,6 +24,10 @@ def post_initialize(options)
@type = "salt"
@states_url = URI(options[:states_url]) if options[:states_url]
@pillar_url = URI(options[:pillar_url]) if options[:pillar_url]
@custom_pillar_root = Pathname(options[:pillar_root]) if options[:pillar_root]
@custom_states_roots = pathnames_from(options[:states_roots])
@custom_formulas_roots = pathnames_from(options[:formulas_roots])
@enabled_states = options.fetch(:enabled_states, [])
end

# Return path to the Salt states directory
Expand All @@ -34,6 +41,7 @@ def states_root(scope = :local)
#
# @return [Pathname] Path to Salt pillars
def pillar_root(scope = :local)
return scoped_paths([@custom_pillar_root], scope).first if @custom_pillar_root
work_dir(scope).join("pillar")
end

Expand All @@ -43,6 +51,46 @@ def pillar_root(scope = :local)
def formulas_root(scope = :local)
work_dir(scope).join("formulas")
end

# Return paths to the states root
#
# @return [Array<Pathname>] Path to Salt state roots
def states_roots(scope = :local)
scoped_paths(@custom_states_roots, scope) + [states_root(scope)]
end

# Return paths to the fromulas root
#
# @return [Array<Pathname>] Path to Salt formulas roots
def formulas_roots(scope = :local)
scoped_paths(@custom_formulas_roots) + [formulas_root(scope)]
end

private

# Convenience method for obtaining the list of given paths relative to
# inst-sys (scope: :local) or to the target system (scope: :target)
#
# @param paths [Array<Pathname>] list of path to be scoped
# @param scope [Symbol] Path relative to inst-sys (:local) or the
# target system (:target)
# @return [Array<Pathname>] list of the given paths prefixed by the
# destination directory in case of :local scope
def scoped_paths(paths, scope = :local)
return paths if scope == :target
prefix = Pathname.new(Installation.destdir)
paths.map { |d| prefix.join(d) }
end

# Convenience method for converting from a list of directory names to a
# list of {Pathname}s
#
# @param dirs [Array<String>] list of directory names
# @return [Array<Pathname>]
def pathnames_from(dirs)
return [] unless dirs.is_a?(Array)
dirs.map { |d| Pathname.new(d) }
end
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions src/lib/configuration_management/configurators/salt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "cheetah"
require "configuration_management/cfa/minion"
require "configuration_management/configurators/base"
require "y2configuration_management/salt/formula_sequence"
require "pathname"

module Yast
Expand All @@ -18,8 +19,7 @@ class Salt < Base
fetch_config(config.states_url, config.work_dir) if config.states_url
fetch_config(config.pillar_url, config.pillar_root) if config.pillar_url
update_configuration
Yast::WFM.CallFunction("configuration_management_formula",
[config.states_root.to_s, config.formulas_root.to_s, config.pillar_root.to_s])
Y2ConfigurationManagement::Salt::FormulaSequence.new(config).run == :finish
end

mode(:client) do
Expand Down Expand Up @@ -62,7 +62,9 @@ def update_configuration
if config.master.is_a?(::String)
config_file.master = config.master
else
config_file.set_file_roots([config.states_root(:target), config.formulas_root(:target)])
config_file.set_file_roots(
config.states_roots(:target) + config.formulas_roots(:target)
)
end
config_file.save
end
Expand Down
85 changes: 0 additions & 85 deletions src/lib/y2configuration_management/clients/formula.rb

This file was deleted.

85 changes: 85 additions & 0 deletions src/lib/y2configuration_management/clients/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright (c) [2018] 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.

require "yast"
require "configuration_management/clients/provision"
require "configuration_management/configurators/salt"
require "configuration_management/configurations/salt"
require "y2configuration_management/salt/formula"

Yast.import "WFM"
Yast.import "PackageSystem"

module Y2ConfigurationManagement
module Clients
# Basic client to run the configuration management tools
#
# It reads the configuration from an XML file.
#
# @example Configuration example
# <configuration_management>
# <type>salt</type>
# <states_roots config:type="list">
# <listitem>/srv/salt</listitem>
# </states_roots>
# <formulas_roots config:type="list">
# <listitem>/srv/formulas</listitem>
# </formulas_roots>
# <pillar_root>/srv/pillar</pillar_root>
# </configuration_management>
class Main < Yast::Client
include Yast::Logger

DEFAULT_SETTINGS = {
"type" => "salt",
"formulas_roots" => Y2ConfigurationManagement::Salt::Formula.formula_directories,
"states_roots" => [
Y2ConfigurationManagement::Salt::Formula::BASE_DIR + "/states",
"/srv/salt/"
],
"pillar_root" => Y2ConfigurationManagement::Salt::Formula::DATA_DIR + "/pillar"
}.freeze

# Runs the client
def run
settings = settings_from_xml || DEFAULT_SETTINGS
log.info("Provisioning Configuration Management")
config = Yast::ConfigurationManagement::Configurations::Base.import(settings)
configurator = Yast::ConfigurationManagement::Configurators::Base.for(config)
return :abort unless configurator.prepare
if !Yast::PackageSystem.CheckAndInstallPackages(configurator.packages.fetch("install", []))
return :abort
end
Yast::ConfigurationManagement::Clients::Provision.new.run
end

private

# Reads the module settings from an XML file
#
# @return [Hash,nil]
def settings_from_xml
filename = Yast::WFM.Args(0)
return nil unless filename && File.exist?(filename)
content = Yast::XML.XMLToYCPFile(filename)
content && content["configuration_management"]
end
end
end
end
27 changes: 17 additions & 10 deletions src/lib/y2configuration_management/salt/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Formula
# @see https://www.suse.com/documentation/suse-manager-3/singlehtml/book_suma_best_practices_31/book_suma_best_practices_31.html#best.practice.salt.formulas.req
DATA_DIR = "/srv/susemanager/formula_data".freeze

# @return [String] Formula path
# @return [Pathname] Formula path
attr_reader :path

# @return [Metadata] Formula metadata
Expand All @@ -55,12 +55,12 @@ class Formula

# Constructor
#
# @param path [String]
# @param path [Pathname]
# @param pillar [Pillar] associated formula data
def initialize(path, pillar = nil)
@path = path
@metadata = Metadata.from_file(File.join(@path, "metadata.yml"))
@form = Form.from_file(File.join(@path, "form.yml"))
@metadata = Metadata.from_file(@path.join("metadata.yml"))
@form = Form.from_file(@path.join("form.yml"))
@pillar = pillar
@enabled = false
end
Expand Down Expand Up @@ -90,14 +90,21 @@ def description

# Return all the installed formulas
#
# @note The result is cached. To force refreshing the cache, set the `reload`
# parameter to `true`.
#
# @param paths [Array<String>|String] File system paths to search for formulas
# @param reload [Boolean] Refresh formulas cache
# @return [Array<Formula>]
def self.all(*paths)
def self.all(*paths, reload: false)
return @formulas if @formulas && !reload
metadata_paths = paths.flatten.compact.empty? ? formula_directories : paths.flatten.compact
Dir.glob(metadata_paths.map { |p| p + "/*" })
.map { |p| Pathname.new(p) }
.select(&:directory?)
.map { |p| Formula.new(p) }
.select(&:form)
@formulas =
Dir.glob(metadata_paths.map { |p| p + "/*" })
.map { |p| Pathname.new(p) }
.select(&:directory?)
.map { |p| Formula.new(p) }
.select(&:form)
end

# Return formula default directories
Expand Down

0 comments on commit 1fe6dcd

Please sign in to comment.