This commit is contained in:
14
Sources/PlaybookClient/Constants.swift
Normal file
14
Sources/PlaybookClient/Constants.swift
Normal file
@@ -0,0 +1,14 @@
|
||||
// TODO: Use an actual version tag for playbook repo.
|
||||
// TODO: Use an externally public url for the playbook repo.
|
||||
|
||||
public extension PlaybookClient {
|
||||
@_spi(Internal)
|
||||
enum Constants {
|
||||
public static let defaultInstallationPath = "~/.local/share/hpa/playbook"
|
||||
public static let inventoryFileName = "inventory.ini"
|
||||
public static let playbookCommand = "ansible-playbook"
|
||||
public static let playbookFileName = "main.yml"
|
||||
public static let playbookRepoUrl = "https://git.housh.dev/michael/ansible-hpa-playbook.git"
|
||||
public static let playbookRepoVersion = "main"
|
||||
}
|
||||
}
|
||||
79
Sources/PlaybookClient/PlaybookClient+Repository.swift
Normal file
79
Sources/PlaybookClient/PlaybookClient+Repository.swift
Normal file
@@ -0,0 +1,79 @@
|
||||
import CommandClient
|
||||
import ConfigurationClient
|
||||
import Dependencies
|
||||
import FileClient
|
||||
import Foundation
|
||||
import ShellClient
|
||||
|
||||
extension PlaybookClient.Repository {
|
||||
|
||||
static func findDirectory(
|
||||
configuration: Configuration? = nil
|
||||
) async throws -> String {
|
||||
@Dependency(\.configurationClient) var configurationClient
|
||||
|
||||
var configuration: Configuration! = configuration
|
||||
if configuration == nil {
|
||||
configuration = try await configurationClient.findAndLoad()
|
||||
}
|
||||
|
||||
return configuration.playbook?.directory
|
||||
?? PlaybookClient.Constants.defaultInstallationPath
|
||||
}
|
||||
|
||||
static func installPlaybook(
|
||||
configuration: Configuration?
|
||||
) async throws {
|
||||
@Dependency(\.commandClient) var commandClient
|
||||
@Dependency(\.configurationClient) var configurationClient
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
var configuration: Configuration! = configuration
|
||||
|
||||
if configuration == nil {
|
||||
configuration = try await configurationClient.findAndLoad()
|
||||
}
|
||||
|
||||
let (path, version) = parsePlaybookPathAndVerion(
|
||||
configuration?.playbook
|
||||
)
|
||||
|
||||
let parentDirectory = URL(filePath: path)
|
||||
.deletingLastPathComponent()
|
||||
|
||||
let parentExists = try await fileClient.isDirectory(parentDirectory)
|
||||
if !parentExists {
|
||||
try await fileClient.createDirectory(parentDirectory)
|
||||
}
|
||||
|
||||
let playbookExists = try await fileClient.isDirectory(URL(filePath: path))
|
||||
|
||||
if !playbookExists {
|
||||
logger.debug("Cloning playbook to: \(path)")
|
||||
try await commandClient.run(
|
||||
"git", "clone", "--branch", version,
|
||||
PlaybookClient.Constants.playbookRepoUrl, path
|
||||
)
|
||||
} else {
|
||||
logger.debug("Playbook exists, ensuring it's up to date.")
|
||||
try await commandClient.run(
|
||||
in: path,
|
||||
"git", "pull", "--tags"
|
||||
)
|
||||
try await commandClient.run(
|
||||
in: path,
|
||||
"git", "checkout", version
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
static func parsePlaybookPathAndVerion(
|
||||
_ configuration: Configuration.Playbook?
|
||||
) -> (path: String, version: String) {
|
||||
return (
|
||||
path: configuration?.directory ?? PlaybookClient.Constants.defaultInstallationPath,
|
||||
version: configuration?.version ?? PlaybookClient.Constants.playbookRepoVersion
|
||||
)
|
||||
}
|
||||
}
|
||||
245
Sources/PlaybookClient/PlaybookClient+RunPlaybook.swift
Normal file
245
Sources/PlaybookClient/PlaybookClient+RunPlaybook.swift
Normal file
@@ -0,0 +1,245 @@
|
||||
import CommandClient
|
||||
import ConfigurationClient
|
||||
import Dependencies
|
||||
import Foundation
|
||||
|
||||
extension PlaybookClient.RunPlaybook.BuildOptions {
|
||||
|
||||
func run() async throws {
|
||||
try await shared.run { arguments, _ in
|
||||
let projectDirectory = projectDirectory
|
||||
?? ProcessInfo.processInfo.environment["PWD"]
|
||||
|
||||
guard let projectDirectory else {
|
||||
throw PlaybookClientError.projectDirectoryNotFound
|
||||
}
|
||||
|
||||
arguments.append(contentsOf: [
|
||||
"--tags", "build-project",
|
||||
"--extra-vars", "project_dir=\(projectDirectory)"
|
||||
])
|
||||
|
||||
if let extraOptions = shared.extraOptions {
|
||||
arguments.append(contentsOf: extraOptions)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PlaybookClient.RunPlaybook.CreateOptions {
|
||||
|
||||
func run(encoder jsonEncoder: JSONEncoder?) async throws {
|
||||
try await shared.run { arguments, configuration in
|
||||
let jsonData = try createJSONData(
|
||||
configuration: configuration,
|
||||
encoder: jsonEncoder
|
||||
)
|
||||
|
||||
guard let json = String(data: jsonData, encoding: .utf8) else {
|
||||
throw PlaybookClientError.encodingError
|
||||
}
|
||||
|
||||
arguments.append(contentsOf: [
|
||||
"--tags", "setup-project",
|
||||
"--extra-vars", "project_dir=\(projectDirectory)",
|
||||
"--extra-vars", "'\(json)'"
|
||||
])
|
||||
|
||||
if let extraOptions = shared.extraOptions {
|
||||
arguments.append(contentsOf: extraOptions)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PlaybookClient.RunPlaybook.GenerateTemplateOptions {
|
||||
func run() async throws -> String {
|
||||
try await shared.run { arguments, _ in
|
||||
arguments.append(contentsOf: [
|
||||
"--tags", "repo-template",
|
||||
"--extra-vars", "output_dir=\(templateDirectory)"
|
||||
])
|
||||
|
||||
if let templateVarsDirectory {
|
||||
arguments.append(contentsOf: ["--extra-vars", "repo_vars_dir=\(templateVarsDirectory)"])
|
||||
}
|
||||
|
||||
if !useVault {
|
||||
arguments.append(contentsOf: ["--extra-vars", "use_vault=false"])
|
||||
}
|
||||
|
||||
return templateDirectory
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PlaybookClient.RunPlaybook.SharedRunOptions {
|
||||
|
||||
@discardableResult
|
||||
func run<T>(
|
||||
_ apply: @Sendable @escaping (inout [String], Configuration) throws -> T
|
||||
) async throws -> T {
|
||||
@Dependency(\.commandClient) var commandClient
|
||||
|
||||
return try await commandClient.run(
|
||||
logging: loggingOptions,
|
||||
quiet: quiet,
|
||||
shell: shell
|
||||
) {
|
||||
@Dependency(\.logger) var logger
|
||||
@Dependency(\.configurationClient) var configurationClient
|
||||
|
||||
let configuration = try await configurationClient.findAndLoad()
|
||||
|
||||
var arguments = try await PlaybookClient.RunPlaybook.makeCommonArguments(
|
||||
configuration: configuration,
|
||||
inventoryFilePath: inventoryFilePath
|
||||
)
|
||||
|
||||
let output = try apply(&arguments, configuration)
|
||||
|
||||
return (arguments, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public extension PlaybookClient.RunPlaybook {
|
||||
|
||||
static func ensuredInventoryPath(
|
||||
_ optionalInventoryPath: String?,
|
||||
configuration: Configuration,
|
||||
playbookDirectory: String
|
||||
) -> String {
|
||||
guard let path = optionalInventoryPath else {
|
||||
guard let path = configuration.playbook?.inventory else {
|
||||
return "\(playbookDirectory)/\(PlaybookClient.Constants.inventoryFileName)"
|
||||
}
|
||||
return path
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
static func makeCommonArguments(
|
||||
configuration: Configuration,
|
||||
inventoryFilePath: String?
|
||||
) async throws -> [String] {
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let playbookDirectory = try await PlaybookClient.Repository.findDirectory(configuration: configuration)
|
||||
let playbookPath = "\(playbookDirectory)/\(PlaybookClient.Constants.playbookFileName)"
|
||||
logger.trace("Playbook path: \(playbookPath)")
|
||||
|
||||
let inventoryPath = ensuredInventoryPath(
|
||||
inventoryFilePath,
|
||||
configuration: configuration,
|
||||
playbookDirectory: playbookDirectory
|
||||
)
|
||||
logger.trace("Inventory path: \(inventoryPath)")
|
||||
|
||||
var arguments = [
|
||||
PlaybookClient.Constants.playbookCommand, playbookPath,
|
||||
"--inventory", inventoryPath
|
||||
]
|
||||
|
||||
if let defaultArgs = configuration.args {
|
||||
arguments.append(contentsOf: defaultArgs)
|
||||
}
|
||||
|
||||
if configuration.useVaultArgs, let vaultArgs = configuration.vault.args {
|
||||
arguments.append(contentsOf: vaultArgs)
|
||||
}
|
||||
|
||||
logger.trace("Common arguments: \(inventoryPath)")
|
||||
return arguments
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
// NOTE: We're not using the `Coders` client because we generally do not
|
||||
// want the output to be `prettyPrinted` or anything, unless we're running
|
||||
// tests, so we use a supplied json encoder.
|
||||
//
|
||||
@_spi(Internal)
|
||||
public extension PlaybookClient.RunPlaybook.CreateOptions {
|
||||
|
||||
func createJSONData(
|
||||
configuration: Configuration,
|
||||
encoder: JSONEncoder?
|
||||
) throws -> Data {
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let encoder = encoder ?? jsonEncoder
|
||||
|
||||
let templateDir = template?.directory ?? configuration.template.directory
|
||||
let templateRepo = template?.url ?? configuration.template.url
|
||||
let version = template?.version ?? configuration.template.version
|
||||
|
||||
logger.debug("""
|
||||
(\(useLocalTemplateDirectory), \(String(describing: templateDir)), \(String(describing: templateRepo)))
|
||||
""")
|
||||
|
||||
switch (useLocalTemplateDirectory, templateDir, templateRepo) {
|
||||
case (true, .none, _):
|
||||
// User supplied they wanted to use a local template directory, but we could not find
|
||||
// the path set from command line or in configuration.
|
||||
throw PlaybookClientError.templateDirectoryNotFound
|
||||
case let (false, _, .some(repo)):
|
||||
// User did not supply they wanted to use a local template directory, and we found a repo url that was
|
||||
// either set by the command line or found in the configuration.
|
||||
logger.debug("Using repo.")
|
||||
return try encoder.encode(TemplateRepo(repo: repo, version: version))
|
||||
case let (true, .some(templateDir), _):
|
||||
// User supplied they wanted to use a local template directory, and we found the template directory
|
||||
// either set by the command line or in the configuration.
|
||||
logger.debug("Using template directory.")
|
||||
return try encoder.encode(TemplateDirJson(path: templateDir))
|
||||
case let (false, .some(templateDir), _):
|
||||
// User supplied they did not wanted to use a local template directory, and we found the template directory
|
||||
// either set by the command line or in the configuration, and no repo was found / handled previously.
|
||||
logger.debug("Using template directory.")
|
||||
return try encoder.encode(TemplateDirJson(path: templateDir))
|
||||
case (_, .none, .none):
|
||||
// We could not find a repo or template directory.
|
||||
throw PlaybookClientError.templateDirectoryOrRepoNotSpecified
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct TemplateDirJson: Encodable {
|
||||
|
||||
let template: Template
|
||||
|
||||
init(path: String) {
|
||||
self.template = .init(path: path)
|
||||
}
|
||||
|
||||
struct Template: Encodable {
|
||||
let path: String
|
||||
}
|
||||
}
|
||||
|
||||
private struct TemplateRepo: Encodable {
|
||||
|
||||
let template: Template
|
||||
|
||||
init(repo: String, version: String?) {
|
||||
self.template = .init(repo: .init(url: repo, version: version ?? "main"))
|
||||
}
|
||||
|
||||
struct Template: Encodable {
|
||||
let repo: Repo
|
||||
}
|
||||
|
||||
struct Repo: Encodable {
|
||||
let url: String
|
||||
let version: String
|
||||
}
|
||||
}
|
||||
|
||||
private let jsonEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.withoutEscapingSlashes, .sortedKeys]
|
||||
return encoder
|
||||
}()
|
||||
262
Sources/PlaybookClient/PlaybookClient.swift
Normal file
262
Sources/PlaybookClient/PlaybookClient.swift
Normal file
@@ -0,0 +1,262 @@
|
||||
import CommandClient
|
||||
import ConfigurationClient
|
||||
import Dependencies
|
||||
import DependenciesMacros
|
||||
import FileClient
|
||||
import Foundation
|
||||
import ShellClient
|
||||
|
||||
// TODO: Add update checks and pull for the playbook.
|
||||
|
||||
public extension DependencyValues {
|
||||
|
||||
/// Manages interactions with the playbook repository as well as `ansible-playbook`
|
||||
/// command line application.
|
||||
///
|
||||
///
|
||||
var playbookClient: PlaybookClient {
|
||||
get { self[PlaybookClient.self] }
|
||||
set { self[PlaybookClient.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
@DependencyClient
|
||||
public struct PlaybookClient: Sendable {
|
||||
|
||||
/// Manages interactions with the playbook repository.
|
||||
public var repository: Repository
|
||||
|
||||
/// Run the `ansible-playbook` command line application.
|
||||
public var run: RunPlaybook
|
||||
}
|
||||
|
||||
public extension PlaybookClient {
|
||||
|
||||
/// Manages interactions with the `ansible-hpa-playbook` repository, which is
|
||||
/// used to build and generate home performance assessment projects.
|
||||
@DependencyClient
|
||||
struct Repository: Sendable {
|
||||
|
||||
/// Install the repository based on the given configuration. If configuration is
|
||||
/// not supplied then the default location for the playbook repository is
|
||||
/// `~/.local/share/hpa/playbook`
|
||||
public var install: @Sendable (Configuration?) async throws -> Void
|
||||
|
||||
/// Get the current directory of the playbook repository based on the given
|
||||
/// configuration.
|
||||
public var directory: @Sendable (Configuration?) async throws -> String
|
||||
|
||||
/// Install the playbook in the default location.
|
||||
public func install() async throws {
|
||||
try await install(nil)
|
||||
}
|
||||
|
||||
/// Get the current directory path of the playbook repository.
|
||||
public func directory() async throws -> String {
|
||||
try await directory(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public extension PlaybookClient {
|
||||
|
||||
/// Runs the `ansible-playbook` command line application with the `ansible-hpa-playbook`.
|
||||
///
|
||||
/// This is used to build and create home performance projects. It can also generate a
|
||||
/// template for a user to customize to their use case.
|
||||
@DependencyClient
|
||||
struct RunPlaybook: Sendable {
|
||||
|
||||
/// Build a home performance assesment project with the given options.
|
||||
public var buildProject: @Sendable (BuildOptions) async throws -> Void
|
||||
|
||||
/// Create a new home performance assesment project with the given options.
|
||||
public var createProject: @Sendable (CreateOptions, JSONEncoder?) async throws -> Void
|
||||
|
||||
/// Generate a user's template from the default home performance template.
|
||||
public var generateTemplate: @Sendable (GenerateTemplateOptions) async throws -> String
|
||||
|
||||
/// Create a new home performance assesment project with the given options.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - options: The options used to create the project.
|
||||
public func createProject(_ options: CreateOptions) async throws {
|
||||
try await createProject(options, nil)
|
||||
}
|
||||
|
||||
/// Represents options that are shared for all the `ansible-playbook` commands.
|
||||
public struct SharedRunOptions: Equatable, Sendable {
|
||||
|
||||
/// Extra arguments / options passed to the `ansible-playbook` command.
|
||||
let extraOptions: [String]?
|
||||
|
||||
/// Specify the inventory file path.
|
||||
let inventoryFilePath: String?
|
||||
|
||||
/// The logging options used when running the command.
|
||||
let loggingOptions: LoggingOptions
|
||||
|
||||
/// Disables log output of the command.
|
||||
let quiet: Bool
|
||||
|
||||
/// Optional shell to use when running the command.
|
||||
let shell: String?
|
||||
|
||||
/// Create the shared options.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - extraOptions: The extra arguments / options to pass to the command.
|
||||
/// - inventoryFilePath: Specify the inventory file path.
|
||||
/// - loggingOptions: The logging options used when running the command.
|
||||
/// - quiet: Disable log output from the command.
|
||||
/// - shell: Specify a shell used when running the command.
|
||||
public init(
|
||||
extraOptions: [String]? = nil,
|
||||
inventoryFilePath: String? = nil,
|
||||
loggingOptions: LoggingOptions,
|
||||
quiet: Bool = false,
|
||||
shell: String? = nil
|
||||
) {
|
||||
self.extraOptions = extraOptions
|
||||
self.inventoryFilePath = inventoryFilePath
|
||||
self.loggingOptions = loggingOptions
|
||||
self.quiet = quiet
|
||||
self.shell = shell
|
||||
}
|
||||
}
|
||||
|
||||
/// Options require when calling the build project command.
|
||||
@dynamicMemberLookup
|
||||
public struct BuildOptions: Equatable, Sendable {
|
||||
|
||||
/// An optional project directory, if not supplied then we will use the current working directory.
|
||||
let projectDirectory: String?
|
||||
|
||||
/// The shared run options.
|
||||
let shared: SharedRunOptions
|
||||
|
||||
/// Create new build options.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - projectDirectory: The optional project directory to build, if not supplied then we'll use the current working directory.
|
||||
/// - shared: The shared run options.
|
||||
public init(
|
||||
projectDirectory: String? = nil,
|
||||
shared: SharedRunOptions
|
||||
) {
|
||||
self.projectDirectory = projectDirectory
|
||||
self.shared = shared
|
||||
}
|
||||
|
||||
public subscript<T>(dynamicMember keyPath: KeyPath<SharedRunOptions, T>) -> T {
|
||||
shared[keyPath: keyPath]
|
||||
}
|
||||
}
|
||||
|
||||
/// Options required when creating a new home performance assessment project.
|
||||
@dynamicMemberLookup
|
||||
public struct CreateOptions: Equatable, Sendable {
|
||||
/// The directory to generate the new project in.
|
||||
let projectDirectory: String
|
||||
/// Shared run options.
|
||||
let shared: SharedRunOptions
|
||||
/// Custom template configuration to use.
|
||||
let template: Configuration.Template?
|
||||
/// Specify whether we should only use a local template directory to create the project from.
|
||||
let useLocalTemplateDirectory: Bool
|
||||
|
||||
/// Create new create options.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - projectDirectory: The directory to generate the project in.
|
||||
/// - shared: The shared run options.
|
||||
/// - template: Custom template configuration used when generating the project.
|
||||
/// - useLocalTemplateDirectory: Whether to use a local template directory, not a template repository.
|
||||
public init(
|
||||
projectDirectory: String,
|
||||
shared: SharedRunOptions,
|
||||
template: Configuration.Template? = nil,
|
||||
useLocalTemplateDirectory: Bool
|
||||
) {
|
||||
self.projectDirectory = projectDirectory
|
||||
self.shared = shared
|
||||
self.template = template
|
||||
self.useLocalTemplateDirectory = useLocalTemplateDirectory
|
||||
}
|
||||
|
||||
public subscript<T>(dynamicMember keyPath: KeyPath<SharedRunOptions, T>) -> T {
|
||||
shared[keyPath: keyPath]
|
||||
}
|
||||
}
|
||||
|
||||
/// Options required when generating a new template repository / directory.
|
||||
@dynamicMemberLookup
|
||||
public struct GenerateTemplateOptions: Equatable, Sendable {
|
||||
/// The shared run options.
|
||||
let shared: SharedRunOptions
|
||||
/// The path to generate the template in.
|
||||
let templateDirectory: String
|
||||
/// Specify the name of the directory for template variables.
|
||||
let templateVarsDirectory: String?
|
||||
/// Specify whether to use `ansible-vault` encrypted variables.
|
||||
let useVault: Bool
|
||||
|
||||
/// Create new generate template options.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - shared: The shared run options
|
||||
/// - templateDirectory: The path to generate the template in.
|
||||
/// - templateVarsDirectory: Specify the name of the directory for template variables.
|
||||
/// - useVault: Specify wheter to use `ansible-vault` encrypted variables.
|
||||
public init(
|
||||
shared: SharedRunOptions,
|
||||
templateDirectory: String,
|
||||
templateVarsDirectory: String? = nil,
|
||||
useVault: Bool = true
|
||||
) {
|
||||
self.shared = shared
|
||||
self.templateDirectory = templateDirectory
|
||||
self.templateVarsDirectory = templateVarsDirectory
|
||||
self.useVault = useVault
|
||||
}
|
||||
|
||||
public subscript<T>(dynamicMember keyPath: KeyPath<SharedRunOptions, T>) -> T {
|
||||
shared[keyPath: keyPath]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PlaybookClient.Repository: DependencyKey {
|
||||
public static var liveValue: Self {
|
||||
.init {
|
||||
try await installPlaybook(configuration: $0)
|
||||
} directory: {
|
||||
try await findDirectory(configuration: $0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PlaybookClient.RunPlaybook: DependencyKey {
|
||||
public static var liveValue: PlaybookClient.RunPlaybook {
|
||||
.init(
|
||||
buildProject: { try await $0.run() },
|
||||
createProject: { try await $0.run(encoder: $1) },
|
||||
generateTemplate: { try await $0.run() }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension PlaybookClient: DependencyKey {
|
||||
public static let testValue: PlaybookClient = Self(
|
||||
repository: Repository(),
|
||||
run: RunPlaybook()
|
||||
)
|
||||
|
||||
public static var liveValue: PlaybookClient {
|
||||
.init(
|
||||
repository: .liveValue,
|
||||
run: .liveValue
|
||||
)
|
||||
}
|
||||
}
|
||||
8
Sources/PlaybookClient/PlaybookClientError.swift
Normal file
8
Sources/PlaybookClient/PlaybookClientError.swift
Normal file
@@ -0,0 +1,8 @@
|
||||
import Foundation
|
||||
|
||||
enum PlaybookClientError: Error {
|
||||
case encodingError
|
||||
case projectDirectoryNotFound
|
||||
case templateDirectoryNotFound
|
||||
case templateDirectoryOrRepoNotSpecified
|
||||
}
|
||||
Reference in New Issue
Block a user