Skip to content

Commit

Permalink
docs: fmt, update service api usage
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Suraci <alex@dagger.io>
  • Loading branch information
vito committed Jul 18, 2023
1 parent bca9a84 commit 499ce97
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 165 deletions.
10 changes: 5 additions & 5 deletions docs/current/guides/snippets/aws-cdk-ecs/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ func (c *AWSClient) cdkDeployStack(ctx context.Context, client *dagger.Client, s
WithExec(cdkCommand).
Sync(ctx)

if err != nil {
var exErr *dagger.ExecError
if errors.As(err, &exErr) {
return nil, fmt.Errorf("cdk deploy exited with code %d", exErr.ExitCode)
}
if err != nil {
var exErr *dagger.ExecError
if errors.As(err, &exErr) {
return nil, fmt.Errorf("cdk deploy exited with code %d", exErr.ExitCode)
}
return nil, err
}

Expand Down
134 changes: 67 additions & 67 deletions docs/current/guides/snippets/build-test-publish-java-spring/main.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
package main

import (
"context"
"fmt"
"log"
"os"
"context"
"fmt"
"log"
"os"

"dagger.io/dagger"
"dagger.io/dagger"
)

func main() {

// check for Docker Hub registry credentials in host environment
vars := []string{"DOCKERHUB_USERNAME", "DOCKERHUB_PASSWORD"}
for _, v := range vars {
if os.Getenv(v) == "" {
log.Fatalf("Environment variable %s is not set", v)
}
}
// check for Docker Hub registry credentials in host environment
vars := []string{"DOCKERHUB_USERNAME", "DOCKERHUB_PASSWORD"}
for _, v := range vars {
if os.Getenv(v) == "" {
log.Fatalf("Environment variable %s is not set", v)
}
}

// initialize Dagger client
ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
panic(err)
}
defer client.Close()
// initialize Dagger client
ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
panic(err)
}
defer client.Close()

// set registry password as secret for Dagger pipeline
password := client.SetSecret("password", os.Getenv("DOCKERHUB_PASSWORD"))
username := os.Getenv("DOCKERHUB_USERNAME")
// set registry password as secret for Dagger pipeline
password := client.SetSecret("password", os.Getenv("DOCKERHUB_PASSWORD"))
username := os.Getenv("DOCKERHUB_USERNAME")

// create a cache volume for Maven downloads
mavenCache := client.CacheVolume("maven-cache")
// create a cache volume for Maven downloads
mavenCache := client.CacheVolume("maven-cache")

// get reference to source code directory
source := client.Host().Directory(".", dagger.HostDirectoryOpts{
Exclude: []string{"ci"},
})
// get reference to source code directory
source := client.Host().Directory(".", dagger.HostDirectoryOpts{
Exclude: []string{"ci"},
})

// create database service container
mariadb := client.Container().
From("mariadb:10.11.2").
WithEnvVariable("MARIADB_USER", "petclinic").
WithEnvVariable("MARIADB_PASSWORD", "petclinic").
WithEnvVariable("MARIADB_DATABASE", "petclinic").
WithEnvVariable("MARIADB_ROOT_PASSWORD", "root").
WithExposedPort(3306).
WithExec([]string{})
// create database service container
mariadb := client.Container().
From("mariadb:10.11.2").
WithEnvVariable("MARIADB_USER", "petclinic").
WithEnvVariable("MARIADB_PASSWORD", "petclinic").
WithEnvVariable("MARIADB_DATABASE", "petclinic").
WithEnvVariable("MARIADB_ROOT_PASSWORD", "root").
WithExposedPort(3306).
WithExec([]string{})

// use maven:3.9 container
// mount cache and source code volumes
// set working directory
app := client.Container().
From("maven:3.9-eclipse-temurin-17").
WithMountedCache("/root/.m2", mavenCache).
WithMountedDirectory("/app", source).
WithWorkdir("/app")
// use maven:3.9 container
// mount cache and source code volumes
// set working directory
app := client.Container().
From("maven:3.9-eclipse-temurin-17").
WithMountedCache("/root/.m2", mavenCache).
WithMountedDirectory("/app", source).
WithWorkdir("/app")

// define binding between
// application and service containers
// define JDBC URL for tests
// test, build and package application as JAR
build := app.WithServiceBinding("db", mariadb).
WithEnvVariable("MYSQL_URL", "jdbc:mysql://petclinic:petclinic@db/petclinic").
WithExec([]string{"mvn", "-Dspring.profiles.active=mysql", "clean", "package"})
// define binding between
// application and service containers
// define JDBC URL for tests
// test, build and package application as JAR
build := app.WithServiceBinding("db", mariadb).
WithEnvVariable("MYSQL_URL", "jdbc:mysql://petclinic:petclinic@db/petclinic").
WithExec([]string{"mvn", "-Dspring.profiles.active=mysql", "clean", "package"})

// use eclipse alpine container as base
// copy JAR files from builder
// set entrypoint and database profile
deploy := client.Container().
From("eclipse-temurin:17-alpine").
WithDirectory("/app", build.Directory("./target")).
WithEntrypoint([]string{"java", "-jar", "-Dspring.profiles.active=mysql", "/app/spring-petclinic-3.0.0-SNAPSHOT.jar"})
// use eclipse alpine container as base
// copy JAR files from builder
// set entrypoint and database profile
deploy := client.Container().
From("eclipse-temurin:17-alpine").
WithDirectory("/app", build.Directory("./target")).
WithEntrypoint([]string{"java", "-jar", "-Dspring.profiles.active=mysql", "/app/spring-petclinic-3.0.0-SNAPSHOT.jar"})

// publish image to registry
address, err := deploy.WithRegistryAuth("docker.io", username, password).
Publish(ctx, fmt.Sprintf("%s/myapp", username))
if err != nil {
panic(err)
}
// publish image to registry
address, err := deploy.WithRegistryAuth("docker.io", username, password).
Publish(ctx, fmt.Sprintf("%s/myapp", username))
if err != nil {
panic(err)
}

// print image address
fmt.Println("Image published at:", address)
// print image address
fmt.Println("Image published at:", address)
}
167 changes: 83 additions & 84 deletions docs/current/guides/snippets/gitlab-google-cloud/main.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,95 @@
package main

import (
"context"
"fmt"
"os"

"dagger.io/dagger"
run "cloud.google.com/go/run/apiv2"
runpb "cloud.google.com/go/run/apiv2/runpb"
"context"
"fmt"
"os"

run "cloud.google.com/go/run/apiv2"
runpb "cloud.google.com/go/run/apiv2/runpb"
"dagger.io/dagger"
)

const GCR_SERVICE_URL = "projects/PROJECT/locations/us-central1/services/myapp"
const GCR_PUBLISH_ADDRESS = "gcr.io/PROJECT/myapp"

func main() {
// create Dagger client
ctx := context.Background()
daggerClient, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
panic(err)
}
defer daggerClient.Close()

// get working directory on host
source := daggerClient.Host().Directory(".", dagger.HostDirectoryOpts{
Exclude: []string{"ci"},
})

// build application
builder := daggerClient.Container(dagger.ContainerOpts{Platform: "linux/amd64"}).
From("golang:1.20").
WithDirectory("/src", source).
WithWorkdir("/src").
WithEnvVariable("CGO_ENABLED", "0").
WithExec([]string{"go", "build", "-o", "myapp"})

// add binary to alpine base
prodImage := daggerClient.Container(dagger.ContainerOpts{Platform: "linux/amd64"}).
From("alpine").
WithFile("/bin/myapp", builder.File("/src/myapp")).
WithEntrypoint([]string{"/bin/myapp"})

// publish container to Google Container Registry
addr, err := prodImage.Publish(ctx, GCR_PUBLISH_ADDRESS)
if err != nil {
panic(err)
}

// print ref
fmt.Println("Published at:", addr)

// create Google Cloud Run client
gcrClient, err := run.NewServicesClient(ctx)
if err != nil {
panic(err)
}
defer gcrClient.Close()

// define service request
gcrRequest := &runpb.UpdateServiceRequest{
Service: &runpb.Service{
Name: GCR_SERVICE_URL,
Template: &runpb.RevisionTemplate{
Containers: []*runpb.Container{
{
Image: addr,
Ports: []*runpb.ContainerPort{
{
Name: "http1",
ContainerPort: 1323,
},
},
},
},
},
},
}

// update service
gcrOperation, err := gcrClient.UpdateService(ctx, gcrRequest)
if err != nil {
panic(err)
}

// wait for service request completion
gcrResponse, err := gcrOperation.Wait(ctx)
if err != nil {
panic(err)
}

// print ref
fmt.Println("Deployment for image", addr, "now available at", gcrResponse.Uri)
// create Dagger client
ctx := context.Background()
daggerClient, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
panic(err)
}
defer daggerClient.Close()

// get working directory on host
source := daggerClient.Host().Directory(".", dagger.HostDirectoryOpts{
Exclude: []string{"ci"},
})

// build application
builder := daggerClient.Container(dagger.ContainerOpts{Platform: "linux/amd64"}).
From("golang:1.20").
WithDirectory("/src", source).
WithWorkdir("/src").
WithEnvVariable("CGO_ENABLED", "0").
WithExec([]string{"go", "build", "-o", "myapp"})

// add binary to alpine base
prodImage := daggerClient.Container(dagger.ContainerOpts{Platform: "linux/amd64"}).
From("alpine").
WithFile("/bin/myapp", builder.File("/src/myapp")).
WithEntrypoint([]string{"/bin/myapp"})

// publish container to Google Container Registry
addr, err := prodImage.Publish(ctx, GCR_PUBLISH_ADDRESS)
if err != nil {
panic(err)
}

// print ref
fmt.Println("Published at:", addr)

// create Google Cloud Run client
gcrClient, err := run.NewServicesClient(ctx)
if err != nil {
panic(err)
}
defer gcrClient.Close()

// define service request
gcrRequest := &runpb.UpdateServiceRequest{
Service: &runpb.Service{
Name: GCR_SERVICE_URL,
Template: &runpb.RevisionTemplate{
Containers: []*runpb.Container{
{
Image: addr,
Ports: []*runpb.ContainerPort{
{
Name: "http1",
ContainerPort: 1323,
},
},
},
},
},
},
}

// update service
gcrOperation, err := gcrClient.UpdateService(ctx, gcrRequest)
if err != nil {
panic(err)
}

// wait for service request completion
gcrResponse, err := gcrOperation.Wait(ctx)
if err != nil {
panic(err)
}

// print ref
fmt.Println("Deployment for image", addr, "now available at", gcrResponse.Uri)

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func main() {
WithDirectory("/srv", client.Directory().WithNewFile("index.html", "Hello, world!")).
WithWorkdir("/srv").
WithExposedPort(8080).
Service([]string{"python", "-m", "http.server", "8080"})
WithExec([]string{"python", "-m", "http.server", "8080"}).
Service()

// create client container with service binding
// access HTTP service and print result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func main() {
WithDirectory("/srv", client.Directory().WithNewFile("index.html", "Hello, world!")).
WithWorkdir("/srv").
WithExposedPort(8080).
Service([]string{"python", "-m", "http.server", "8080"})
WithExec([]string{"python", "-m", "http.server", "8080"}).
Service()

// create client container with service binding
// access HTTP service, write to file and retrieve contents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func main() {
WithDirectory("/srv", client.Directory().WithNewFile("index.html", "Hello, world!")).
WithWorkdir("/srv").
WithExposedPort(8080).
Service([]string{"python", "-m", "http.server", "8080"})
WithExec([]string{"python", "-m", "http.server", "8080"}).
Service()

// get endpoint
val, err := httpSrv.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
WithExposedPort(6379).
WithMountedCache("/data", client.CacheVolume("my-redis")).
WithWorkdir("/data").
Service(nil)
Service()

// create Redis client container
redisCLI := client.Container().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
redisSrv := client.Container().
From("redis").
WithExposedPort(6379).
Service(nil)
Service()

// create Redis client container
redisCLI := client.Container().
Expand Down
Loading

0 comments on commit 499ce97

Please sign in to comment.