Skip to content

Commit

Permalink
Use purga approach for configurable flake deployments (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaiser authored May 6, 2024
1 parent 93d7826 commit c81456e
Showing 1 changed file with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,51 @@ import besom.json.JsonWriter
import scala.concurrent.Future
import Pulumi.given_ExecutionContext

case class ConfigurableFlakeDeployment private (
flakeDir: Output[String]
case class PurgaDeployment private (
config: Output[String]
)(using ComponentBase)
extends ComponentResource
derives RegistersOutputs

object ConfigurableFlakeDeployment:
object PurgaDeployment:

case class Params[A](
flakeRepository: Input[String],
flakeOutput: Input[String],
flake: Input[String],
flakeInput: Input[String] = "purgaArgs",
config: Input[A],
targetHost: Input[String], // including usrname@
configLocation: Input[String] = "./config.json"
targetHost: Input[String] // including usrname@
)

def apply[A: JsonWriter](using Context)(
name: NonEmptyString,
params: Params[A],
options: ComponentResourceOptions = ComponentResourceOptions()
): Output[ConfigurableFlakeDeployment] =
): Output[PurgaDeployment] =
component(
name,
"biser:nix:ConfigurableFlakeDeployment",
"biser:nix:PurgaDeployment",
options
) {
val jsonConfig = params.config.asOutput().map(conf => summon[JsonWriter[A]].write(conf).toString)
val jsonConfig = params.config.asOutput().map { conf => summon[JsonWriter[A]].write(conf).toString }

val deployment = for {

flakeDir <- command.local
.Command(
s"$name-flake-directory",
command.local.CommandArgs(
create = s"mkdir -p '$$XDG_STATE_HOME/biser/flakes/$name' && echo '$$XDG_STATE_HOME/biser/flakes/$name'",
delete = s"rm -rf '$$XDG_STATE_HOME/biser/flakes/$name'"
)
)
.stdout
config <- jsonConfig

_ <- command.local
.Command(
s"$name-deploy",
command.local.CommandArgs(
create =
p"""git clone --depth=1 ${params.flakeRepository} . && echo '$jsonConfig' > '${params.configLocation}' && nixos-rebuild switch < /dev/null --use-remote-sudo --target-host ${params.targetHost} --show-trace --flake ".#${params.flakeOutput}"""",
dir = flakeDir,
p"""f=$$(mktemp); echo '$config' > $$f ; nixos-rebuild switch < /dev/null --use-remote-sudo --target-host ${params.targetHost} --show-trace --flake "${params.flake}";rm-rf $$f""",
environment = Map(
"NIX_SSHOPTS" -> "-t -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
)
)
)
.stdout

} yield flakeDir
} yield config

ConfigurableFlakeDeployment(deployment)
PurgaDeployment(deployment)
}

0 comments on commit c81456e

Please sign in to comment.