Skip to content

Commit

Permalink
Move to distage (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaiser authored Jun 15, 2024
1 parent 15abf90 commit db749b1
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 238 deletions.
18 changes: 10 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
val scala3Version = "3.4.2"

val BesomVersion = "0.3.2"
val BesomProxmoxveVersion = "6.3.1-core.0.3"
val BesomCommandVersion = "0.10.0-core.0.3"
val BesomProxmoxveVersion = "6.3.1-core.0.3"
val BesomVersion = "0.3.2"
val IzumiVersion = "1.2.9"
val ZioVersion = "2.1.3"
val OsLibVersion = "0.10.2"
val ZioInteropCatsVersion = "23.1.0.2"
val ZioVersion = "2.1.3"

inThisBuild(
List(
Expand Down Expand Up @@ -37,12 +38,13 @@ lazy val root = project
name := "biser",
scalaVersion := scala3Version,
libraryDependencies ++= Seq(
"com.lihaoyi" %% "os-lib" % OsLibVersion,
"dev.zio" %% "zio" % ZioVersion,
"dev.zio" %% "zio-interop-cats" % ZioInteropCatsVersion,
"io.7mind.izumi" %% "distage-core" % IzumiVersion,
"org.virtuslab" %% "besom-command" % BesomCommandVersion,
"org.virtuslab" %% "besom-core" % BesomVersion,
"org.virtuslab" %% "besom-zio" % BesomVersion,
"org.virtuslab" %% "besom-proxmoxve" % BesomProxmoxveVersion,
"org.virtuslab" %% "besom-command" % BesomCommandVersion,
"io.7mind.izumi" %% "distage-core" % IzumiVersion,
"dev.zio" %% "zio" % ZioVersion,
"dev.zio" %% "zio-interop-cats" % ZioInteropCatsVersion
"org.virtuslab" %% "besom-zio" % BesomVersion
)
)
3 changes: 3 additions & 0 deletions src/main/scala/com/nikolaiser/biser/common/SshConfig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.nikolaiser.biser.common

case class SshConfig(publicKey: String)
36 changes: 0 additions & 36 deletions src/main/scala/com/nikolaiser/biser/nix/FlakeBuild.scala

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/scala/com/nikolaiser/biser/nix/Image.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nikolaiser.biser.nix

import besom.*
import izumi.distage.model.definition.Id
import besom.api.command.local.Command
import besom.api.command.local.CommandArgs
import com.nikolaiser.biser.common.SshConfig

trait Image:

/** @return
* Path to the image file
*/
def path: Output[String]

object Image:
case class Impl(flake: String @Id("base-image-flake"), sshConfig: SshConfig)(using Context) extends Image:

private val cmd =
s"""purga --arg sshKey='${sshConfig.publicKey}' -- nix build $flake --no-link --refresh --json 2> /dev/null | jq '.[0].outputs.out' --raw-output"""

val path: Output[String] =
Command(s"$flake-base-image-build", CommandArgs(create = cmd)).stdout.map(_ + "/nixos.img")
81 changes: 30 additions & 51 deletions src/main/scala/com/nikolaiser/biser/nix/PurgaDeployment.scala
Original file line number Diff line number Diff line change
@@ -1,56 +1,35 @@
package com.nikolaiser.biser.nix

import besom.*
import besom.api.command
import besom.internal.RegistersOutputs
import besom.api.command.local.Command
import besom.api.command.local.CommandArgs
import besom.json.JsonWriter
import scala.concurrent.Future
import Pulumi.given_ExecutionContext

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

object PurgaDeployment:

case class Params[A](
flake: Input[String],
flakeInput: Input[String] = "purgaArgs",
config: Input[A],
targetHost: Input[String] // including usrname@
import besom.internal.Result
import besom.json.JsString

def purgaDeployment[A](username: String, host: String, flake: String, config: A)(using writer: JsonWriter[A], ctx: Context) =
val jsonConfig = writer.write(config)
val deployCmd =
s"""f=$$(mktemp); echo '${jsonConfig.toString}' > $$f ; nixos-rebuild switch < /dev/null --use-remote-sudo --target-host $username@$host --show-trace --flake "$flake" --override-input purgaArgs file+file://$$f --no-write-lock-file --refresh;rm -rf $$f"""
val checkRevisionCmd = s"nix flake metadata ${flake.split("#").head} --no-write-lock-file --json --refresh | jq '.revision'"

Command(
s"$host-$flake-deploy",
CommandArgs(
create = deployCmd,
update = deployCmd,
environment = Map(
"NIX_SSHOPTS" -> "-t -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
),
triggers = List(
jsonConfig,
besom.internal.Output
.apply(
Result.blocking(
os.proc("/bin/sh", "-c", checkRevisionCmd).spawn().stdout.trim()
)
)
.map(JsString(_))
)
)
)

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

val deployment = for {

config <- jsonConfig

_ <- command.local
.Command(
s"$name-deploy",
command.local.CommandArgs(
create =
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 config

PurgaDeployment(deployment)
}
89 changes: 0 additions & 89 deletions src/main/scala/com/nikolaiser/biser/proxmox/CloudInitVm.scala

This file was deleted.

48 changes: 0 additions & 48 deletions src/main/scala/com/nikolaiser/biser/proxmox/NixOsCloudInit.scala

This file was deleted.

35 changes: 35 additions & 0 deletions src/main/scala/com/nikolaiser/biser/proxmox/NodeImage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.nikolaiser.biser.proxmox

import besom.*
import besom.api.proxmoxve.storage.File
import besom.api.proxmoxve.storage.FileArgs
import besom.api.proxmoxve.storage.inputs.FileSourceFileArgs
import besom.api.proxmoxve.Provider
import com.nikolaiser.biser.nix.Image
import izumi.distage.model.definition.With

trait NodeImage:
def file: Output[File]

object NodeImage:

type Factory = String => NodeImage @With[NodeImage.Impl]

case class Impl(
nodeName: String,
baseImage: Image,
provider: Provider
)(using
Context
) extends NodeImage:
val file: Output[File] =
File(
s"$nodeName-image-upload",
FileArgs(
contentType = "snippets",
datastoreId = "local",
nodeName = nodeName,
sourceFile = FileSourceFileArgs(path = baseImage.path)
),
opts(provider = provider)
)
Loading

0 comments on commit db749b1

Please sign in to comment.