-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15abf90
commit db749b1
Showing
11 changed files
with
157 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.nikolaiser.biser.common | ||
|
||
case class SshConfig(publicKey: String) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
src/main/scala/com/nikolaiser/biser/nix/PurgaDeployment.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
89
src/main/scala/com/nikolaiser/biser/proxmox/CloudInitVm.scala
This file was deleted.
Oops, something went wrong.
48 changes: 0 additions & 48 deletions
48
src/main/scala/com/nikolaiser/biser/proxmox/NixOsCloudInit.scala
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
src/main/scala/com/nikolaiser/biser/proxmox/NodeImage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
Oops, something went wrong.