Skip to content

Commit

Permalink
Add Azure ACI example (#452)
Browse files Browse the repository at this point in the history
* add azure ACI example

---------

Co-authored-by: Łukasz Biały <[email protected]>
  • Loading branch information
polkx and lbialy authored Aug 21, 2024
1 parent b5bf1d8 commit 29ae540
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ compile-cfg-lib: publish-local-json publish-local-core

# Compiles besom-cfg k8s module
compile-cfg-k8s: publish-local-cfg-lib
just cli packages local kubernetes
just cli packages local kubernetes:4.17.1
scala-cli --power compile besom-cfg/k8s --suppress-experimental-feature-warning

# Compiles all besom-cfg modules
Expand Down
2 changes: 1 addition & 1 deletion besom-cfg/k8s/project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//> using dep com.lihaoyi::os-lib::0.9.3
//> using dep org.virtuslab::besom-cfg:0.2.0-SNAPSHOT
//> using dep org.virtuslab::besom-kubernetes:4.15.0-core.0.4-SNAPSHOT
//> using dep org.virtuslab::besom-kubernetes:4.17.1-core.0.4-SNAPSHOT
//> using dep com.lihaoyi::fansi::0.5.0
//> using dep com.lihaoyi::fastparse:3.1.0

Expand Down
9 changes: 4 additions & 5 deletions core/src/main/scala/besom/internal/Output.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package besom.internal

import scala.collection.BuildFrom
import scala.reflect.Typeable

/** Output is a wrapper for a monadic effect used to model async execution that allows Pulumi to track information about dependencies
* between resources and properties of data (whether it's known or a secret for instance).
Expand Down Expand Up @@ -207,14 +206,14 @@ If you want to map over the value of an Output, use the map method instead."""
*/
def void: Output[Unit] = map(_ => ())

private[besom] def flatMapOption[B, C: Typeable](using ev: A <:< Option[B])(f: B => Output[C] | Output[Option[C]]): Output[Option[C]] =
private[besom] def flatMapOption[B, C](using ev: A <:< Option[B])(f: B => Output[C] | Output[Option[C]]): Output[Option[C]] =
flatMap { a =>
ev(a) match
case Some(b) =>
f(b).map {
case Some(c: C) => Some(c)
case None => None
case x: C => Some(x)
case Some(c) => Some(c.asInstanceOf[C])
case None => None
case c => Some(c.asInstanceOf[C])
}
case None => Output(None)
}
Expand Down
10 changes: 10 additions & 0 deletions examples/azure-aci/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### Scala an JVM
*.class
*.log
.bsp
.scala-build

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

kubeconfig.json
44 changes: 44 additions & 0 deletions examples/azure-aci/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import besom.*
import besom.api.azurenative

@main def main = Pulumi.run {
val resourceGroup = azurenative.resources.ResourceGroup("resourceGroup")

val imageName = "mcr.microsoft.com/azuredocs/aci-helloworld"

val containerGroup = azurenative.containerinstance.ContainerGroup(
name = "containerGroup",
azurenative.containerinstance.ContainerGroupArgs(
resourceGroupName = resourceGroup.name,
osType = azurenative.containerinstance.enums.OperatingSystemTypes.Linux,
containers = List(
azurenative.containerinstance.inputs.ContainerArgs(
name = "hello-world",
image = imageName,
ports = List(azurenative.containerinstance.inputs.ContainerPortArgs(port = 80)),
resources = azurenative.containerinstance.inputs.ResourceRequirementsArgs(
requests = azurenative.containerinstance.inputs.ResourceRequestsArgs(
cpu = 1.0,
memoryInGB = 1.5
)
)
)
),
ipAddress = azurenative.containerinstance.inputs.IpAddressArgs(
ports = List(
azurenative.containerinstance.inputs.PortArgs(
port = 80,
protocol = azurenative.containerinstance.enums.ContainerGroupNetworkProtocol.TCP
)
),
`type` = azurenative.containerinstance.enums.ContainerGroupIpAddressType.Public
),
restartPolicy = azurenative.containerinstance.enums.ContainerGroupRestartPolicy.Always
)
)

Stack(containerGroup).exports(
// TODO uncomment when bug https://github.com/VirtusLab/besom/issues/432 will be fixed
containerIPv4Address = containerGroup.ipAddress.ip
)
}
3 changes: 3 additions & 0 deletions examples/azure-aci/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: azure-aci
description: Azure ACI example
runtime: scala
41 changes: 41 additions & 0 deletions examples/azure-aci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Azure Container Instances on Linux

Starting point for building web application hosted in Azure Container Instances.

## Deploying the App

To deploy your infrastructure, follow the below steps.

### Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
2. [Configure Azure Credentials](https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/)

## Running the App

1. Create a new stack:

```
$ pulumi stack init dev
```

2. Set the Azure region location to use:

```
$ pulumi config set azure-native:location westus2
```

3. Stand up the cluster by invoking pulumi
```bash
$ pulumi up
```

4. From there, feel free to experiment. Simply making edits and running `pulumi up` will incrementally update your
stack.

5. Once you've finished experimenting, tear down your stack's resources by destroying and removing it:

```bash
$ pulumi destroy --yes
$ pulumi stack rm --yes
```
8 changes: 8 additions & 0 deletions examples/azure-aci/project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//> using scala "3.3.1"
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement
//> using plugin "org.virtuslab::besom-compiler-plugin:0.4.0-SNAPSHOT"

//> using dep "org.virtuslab::besom-core:0.4.0-SNAPSHOT"
//> using dep "org.virtuslab::besom-azure-native:2.56.0-core.0.4-SNAPSHOT"

//> using repository sonatype:snapshots
12 changes: 9 additions & 3 deletions scripts/Packages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,23 @@ object Packages:
)

lazy val compileLocalOpts: Vector[os.Shellable] =
if isCI
if isCI && CIMaxMem.isDefined
then compileOpts(heapMaxGb = CIMaxMem.get)
else if isCI
then compileOpts(heapMaxGb = 16)
else compileOpts(heapMaxGb = 32)

lazy val localOpts: Vector[os.Shellable] =
if isCI
if isCI && CIMaxMem.isDefined
then publishOpts(heapMaxGb = CIMaxMem.get, jarCompression = 1)
else if isCI
then publishOpts(heapMaxGb = 16, jarCompression = 1)
else publishOpts(heapMaxGb = 32, jarCompression = 1)

lazy val mavenOpts: Vector[os.Shellable] = {
if isCI
if isCI && CIMaxMem.isDefined
then publishOpts(heapMaxGb = CIMaxMem.get, jarCompression = 9, sources = true, docs = true)
else if isCI
then publishOpts(heapMaxGb = 16, jarCompression = 9, sources = true, docs = true)
else publishOpts(heapMaxGb = 32, jarCompression = 9, sources = true, docs = true)
} ++ mavenAuthOpts(pgpKey = envOrExit("PGP_KEY_ID"))
Expand Down
7 changes: 7 additions & 0 deletions scripts/common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ lazy val isCI: Boolean =
println(s"CI: ${ci}")
ci

lazy val CIMaxMem: Option[Int] =
val ciMem = sys.env.get("CI_MAX_MEM").flatMap { str =>
str.toIntOption
}
println(s"CI Max Mem: $ciMem")
ciMem

def githubToken: Option[String] =
val token = sys.env.get("GITHUB_TOKEN")
(isCI, token) match
Expand Down

0 comments on commit 29ae540

Please sign in to comment.