v0.8.32 simplify cli

This commit is contained in:
ansgarz 2021-12-27 21:17:19 +01:00
parent 835d777c90
commit 2ea30ab6c2
6 changed files with 46 additions and 56 deletions

View file

@ -18,7 +18,7 @@ apply plugin: 'kotlinx-serialization'
group = 'org.domaindrivenarchitecture.provs' group = 'org.domaindrivenarchitecture.provs'
version = '0.8.32-SNAPSHOT' version = '0.8.32'
repositories { repositories {
mavenCentral() mavenCentral()

View file

@ -11,7 +11,8 @@ participant User
box "application" #LightBlue box "application" #LightBlue
participant CliWorkplace participant CliWorkplace
participant WorkplaceCliCommand participant CliWorkplaceParser
participant CliWorkplaceCommand
participant Application participant Application
end box end box
@ -19,46 +20,42 @@ end box
box #White box #White
participant CliUtils participant CliUtils
participant "ProvInstance (local, remote...)" as ProvInstance participant "Prov (local or remote...)" as ProvInstance
end box end box
box "domain" #LightGreen box "domain" #LightGreen
participant ProvisionWorkplace participant ProvisionWorkplace
participant WorkplaceConfig
end box end box
box "infrastructure" #CornSilk box "infrastructure" #CornSilk
participant ConfigRepository participant ConfigRepository
participant InfrastructureModules participant "Infrastructure functions" as Infrastructure_functions
end box end box
User -> CliWorkplace ++ : main(args...) User -> CliWorkplace ++ : main(args...)
CliWorkplace -> WorkplaceCliCommand : parseWorkplaceArguments CliWorkplace -> CliWorkplaceParser : parseWorkplaceArguments
CliWorkplace -> WorkplaceCliCommand : isValid ? CliWorkplace -> CliWorkplaceCommand : isValid ?
CliWorkplace -> ConfigRepository : getConfig
CliWorkplace -> CliUtils : createProvInstance CliWorkplace -> CliUtils : createProvInstance
ProvInstance <- CliUtils : create ProvInstance <- CliUtils : create
CliWorkplace -> ConfigRepository : getConfig
WorkplaceConfig <- ConfigRepository : create
WorkplaceConfig --> ConfigRepository : config
CliWorkplace <-- ConfigRepository : config
CliWorkplace -> Application : provision ( config ) CliWorkplace -> Application : provision ( config )
Application -> ProvInstance : provisionWorkplace ( type, ssh, ...) Application -> ProvInstance : provisionWorkplace ( type, ssh, ...)
ProvInstance -> ProvisionWorkplace : provisionWorkplace ProvInstance -> ProvisionWorkplace : provisionWorkplace
ProvisionWorkplace -> InfrastructureModules: Various calls like: ProvisionWorkplace -> Infrastructure_functions: Various calls like:
ProvisionWorkplace -> InfrastructureModules: install ssh, gpg, git ... ProvisionWorkplace -> Infrastructure_functions: install ssh, gpg, git ...
ProvisionWorkplace -> InfrastructureModules: installVirtualBoxGuestAdditions ProvisionWorkplace -> Infrastructure_functions: installVirtualBoxGuestAdditions
ProvisionWorkplace -> InfrastructureModules: configureNoSwappiness, ... ProvisionWorkplace -> Infrastructure_functions: configureNoSwappiness, ...
@enduml @enduml

View file

@ -20,7 +20,7 @@ import kotlin.system.exitProcess
* If the target is remote and if parameter remoteHostSetSudoWithoutPasswordRequired is set to true, * If the target is remote and if parameter remoteHostSetSudoWithoutPasswordRequired is set to true,
* it will enable sudo without password on the remote machine (in case this was not yet enabled). * it will enable sudo without password on the remote machine (in case this was not yet enabled).
*/ */
internal fun createProvInstance( fun createProvInstance(
targetCommand: TargetCliCommand, targetCommand: TargetCliCommand,
remoteHostSetSudoWithoutPasswordRequired: Boolean = false remoteHostSetSudoWithoutPasswordRequired: Boolean = false
): Prov { ): Prov {

View file

@ -1,41 +1,35 @@
package org.domaindrivenarchitecture.provs.workplace.application package org.domaindrivenarchitecture.provs.workplace.application
import org.domaindrivenarchitecture.provs.core.cli.createProvInstance import org.domaindrivenarchitecture.provs.core.cli.createProvInstance
import org.domaindrivenarchitecture.provs.workplace.application.WorkplaceCliCommand.Companion.parseWorkplaceArguments
import org.domaindrivenarchitecture.provs.workplace.infrastructure.getConfig import org.domaindrivenarchitecture.provs.workplace.infrastructure.getConfig
import java.io.File import java.io.File
import kotlin.system.exitProcess import kotlin.system.exitProcess
/** /**
* Provisions a workplace locally or on a remote machine. * Provisions a workplace locally or on a remote machine. Use option -h for help.
* Specify option -h for help.
*/ */
fun main(args: Array<String>) { fun main(args: Array<String>) {
val cmd = parseWorkplaceArguments("java -jar provs.jar", args) val cmd = CliWorkplaceParser("java -jar provs.jar").parseWorkplaceArguments(args)
if (!cmd.isValid()) { if (!cmd.isValid()) {
println("Arguments are not valid, pls try -h for help.") println("Arguments are not valid, pls try option -h for help.")
exitProcess(1) exitProcess(1)
} }
provisionWorkplace(cmd)
}
private fun provisionWorkplace(cliCommand: WorkplaceCliCommand) {
val filename = cliCommand.configFile
try { try {
val conf = getConfig(filename) // retrieve config
val conf = getConfig(cmd.configFile)
val prov = createProvInstance(cliCommand.target, remoteHostSetSudoWithoutPasswordRequired = true) // create
val prov = createProvInstance(cmd.target, remoteHostSetSudoWithoutPasswordRequired = true)
provision(prov, conf) provision(prov, conf)
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
println( println(
"Error: File\u001b[31m $filename \u001b[0m was not found.\n" + "Error: File\u001b[31m ${cmd.configFile} \u001b[0m was not found.\n" +
"Pls copy file \u001B[31m WorkplaceConfigExample.yaml \u001B[0m to file \u001B[31m $filename \u001B[0m " + "Pls copy file \u001B[31m WorkplaceConfigExample.yaml \u001B[0m to file \u001B[31m ${cmd.configFile} \u001B[0m " +
"and change the content according to your needs.\n" "and change the content according to your needs.\n"
) )

View file

@ -4,30 +4,9 @@ import org.domaindrivenarchitecture.provs.core.cli.TargetCliCommand
class WorkplaceCliCommand(val configFile: String, val target: TargetCliCommand) { class WorkplaceCliCommand(val configFile: String, val target: TargetCliCommand) {
companion object {
fun parseWorkplaceArguments(
programName: String = "java -jar provs.jar",
args: Array<String>
): WorkplaceCliCommand {
val parser = CliWorkplaceParser(programName)
parser.parse(args)
return WorkplaceCliCommand(
parser.configFileName ?: "WorkplaceConfig.yaml",
TargetCliCommand(
parser.localHost,
parser.remoteHost,
parser.userName,
parser.sshWithPasswordPrompt,
parser.sshWithGopassPath,
parser.sshWithKey
)
)
}
}
fun isValid(): Boolean { fun isValid(): Boolean {
return target.isValid() return configFile.isNotEmpty() && target.isValid()
} }
} }

View file

@ -3,11 +3,31 @@ package org.domaindrivenarchitecture.provs.workplace.application
import kotlinx.cli.ArgType import kotlinx.cli.ArgType
import kotlinx.cli.optional import kotlinx.cli.optional
import org.domaindrivenarchitecture.provs.core.cli.CliTargetParser import org.domaindrivenarchitecture.provs.core.cli.CliTargetParser
import org.domaindrivenarchitecture.provs.core.cli.TargetCliCommand
open class CliWorkplaceParser(name: String) : CliTargetParser(name) {
class CliWorkplaceParser(name: String) : CliTargetParser(name) {
val configFileName by argument( val configFileName by argument(
ArgType.String, ArgType.String,
"configFilename", "configFilename",
"the filename containing the yaml config for the workplace" "the filename containing the yaml config for the workplace"
).optional() ).optional()
fun parseWorkplaceArguments(args: Array<String>): WorkplaceCliCommand {
super.parse(args)
return WorkplaceCliCommand(
configFileName ?: "WorkplaceConfig.yaml",
TargetCliCommand(
localHost,
remoteHost,
userName,
sshWithPasswordPrompt,
sshWithGopassPath,
sshWithKey
)
)
}
} }