stdlib: add random package

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-06-08 11:46:31 -07:00
parent 4c40a87634
commit db0937c927
24 changed files with 152 additions and 580 deletions

69
stdlib/random/string.cue Normal file
View File

@ -0,0 +1,69 @@
// Random generation utilities.
//
// Example:
// ```
// str: random.#String & {
// seed: "str"
// length: 10
// }
// ```
package random
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
// Generate a random string
#String: {
// Seed of the random string to generate.
// When using the same `seed`, the same random string will be generated
// because of caching.
// FIXME: this is necessary because of https://github.com/dagger/dagger/issues/591
seed: string @dagger(input)
// length of the string
length: *12 | number @dagger(input)
// generated random string
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
length = int(os.environ['LENGTH'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(length)) )
"""#
},
op.#Exec & {
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: LENGTH: strconv.FormatInt(length, 10)
env: SEED: seed
always: true
},
op.#Export & {
source: "/rand"
},
]
} @dagger(output)
}

View File

@ -3,6 +3,7 @@ package main
import (
"dagger.io/dagger/op"
"dagger.io/alpine"
"dagger.io/random"
)
registry: {
@ -11,15 +12,19 @@ registry: {
}
TestPushContainer: {
tag: random.#String & {
seed: "push-container"
}
// Push an image with a random tag
push: {
ref: "daggerio/ci-test:\(random)"
ref: "daggerio/ci-test:\(tag.out)"
#up: [
op.#DockerLogin & {
registry
},
op.#WriteFile & {
content: random
content: tag.out
dest: "/rand"
},
op.#PushContainer & {
@ -41,7 +46,7 @@ TestPushContainer: {
op.#Exec & {
args: [
"sh", "-c", #"""
test "$(cat /src/rand)" = "\#(random)"
test "$(cat /src/rand)" = "\#(tag.out)"
"""#,
]
mount: "/src": from: pull
@ -51,14 +56,18 @@ TestPushContainer: {
// Ensures image metadata is preserved in a push
TestPushContainerMetadata: {
tag: random.#String & {
seed: "container-metadata"
}
// `docker build` using an `ENV` and push the image
push: {
ref: "daggerio/ci-test:\(random)-dockerbuild"
ref: "daggerio/ci-test:\(tag.out)-dockerbuild"
#up: [
op.#DockerBuild & {
dockerfile: #"""
FROM alpine:latest@sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
ENV CHECK \#(random)
ENV CHECK \#(tag.out)
"""#
},
op.#PushContainer & {
@ -76,7 +85,7 @@ TestPushContainerMetadata: {
args: [
"sh", "-c", #"""
env
test "$CHECK" = "\#(random)"
test "$CHECK" = "\#(tag.out)"
"""#,
]
},
@ -85,7 +94,7 @@ TestPushContainerMetadata: {
// Do a FetchContainer followed by a PushContainer, make sure
// the ENV is preserved
pullPush: {
ref: "daggerio/ci-test:\(random)-pullpush"
ref: "daggerio/ci-test:\(tag.out)-pullpush"
#up: [
op.#FetchContainer & {
@ -104,7 +113,7 @@ TestPushContainerMetadata: {
op.#Exec & {
args: [
"sh", "-c", #"""
test "$CHECK" = "\#(random)"
test "$CHECK" = "\#(tag.out)"
"""#,
]
},

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -4,6 +4,7 @@ import (
"dagger.io/aws"
"dagger.io/aws/ecr"
"dagger.io/dagger/op"
"dagger.io/random"
)
TestConfig: awsConfig: aws.#Config & {
@ -11,10 +12,12 @@ TestConfig: awsConfig: aws.#Config & {
}
TestECR: {
random: #Random & {}
suffix: random.#String & {
seed: ""
}
repository: "125635003186.dkr.ecr.\(TestConfig.awsConfig.region).amazonaws.com/dagger-ci"
tag: "test-ecr-\(random.out)"
tag: "test-ecr-\(suffix.out)"
creds: ecr.#Credentials & {
config: TestConfig.awsConfig
@ -27,7 +30,7 @@ TestECR: {
op.#DockerBuild & {
dockerfile: """
FROM alpine
RUN echo \(random.out) > /test
RUN echo \(suffix.out) > /test
"""
},
@ -63,7 +66,7 @@ TestECR: {
op.#Exec & {
always: true
args: [
"sh", "-c", "test $(cat test) = \(random.out)",
"sh", "-c", "test $(cat test) = \(suffix.out)",
]
},
]
@ -78,7 +81,7 @@ TestECR: {
op.#DockerBuild & {
dockerfile: #"""
FROM \#(push.ref)
RUN test $(cat test) = \#(random.out)
RUN test $(cat test) = \#(suffix.out)
"""#
},
]

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -5,6 +5,7 @@ import (
"dagger.io/dagger/op"
"dagger.io/alpine"
"dagger.io/docker"
"dagger.io/random"
)
source: dagger.#Artifact
@ -15,9 +16,11 @@ registry: {
}
TestPushAndPull: {
random: #Random & {}
tag: random.#String & {
seed: ""
}
ref: "daggerio/ci-test:\(random.out)"
ref: "daggerio/ci-test:\(tag.out)"
// Create image
image: docker.#ImageFromDockerfile & {

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -3,13 +3,16 @@ package main
import (
"dagger.io/docker"
"dagger.io/dagger"
"dagger.io/random"
)
TestRun: {
random: #Random & {}
suffix: random.#String & {
seed: ""
}
run: docker.#Run & {
name: "daggerci-test-local-\(random.out)"
name: "daggerci-test-local-\(suffix.out)"
ref: "hello-world"
}
}

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -3,6 +3,7 @@ package docker
import (
"dagger.io/docker"
"dagger.io/dagger"
"dagger.io/random"
)
key: dagger.#Secret @dagger(input)
@ -10,14 +11,16 @@ passphrase: dagger.#Secret @dagger(input)
user: string @dagger(input)
TestRun: {
random: #Random & {}
suffix: random.#String & {
seed: ""
}
run: docker.#Run & {
host: "143.198.64.230"
ref: "nginx:alpine"
"user": user
"passphrase": passphrase
name: "daggerci-test-simple-\(random.out)"
name: "daggerci-test-simple-\(suffix.out)"
"key": key
}
}

View File

@ -3,6 +3,7 @@ package main
import (
"dagger.io/docker"
"dagger.io/dagger"
"dagger.io/random"
)
TestConfig: {
@ -13,11 +14,13 @@ TestConfig: {
}
TestRun: {
random: #Random & {}
suffix: random.#String & {
seed: ""
}
run: docker.#Run & {
ref: "hello-world"
name: "daggerci-test-simple-\(random.out)"
name: "daggerci-test-simple-\(suffix.out)"
ssh: {
host: TestConfig.host

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -3,6 +3,7 @@ package main
import (
"dagger.io/docker"
"dagger.io/dagger"
"dagger.io/random"
)
TestConfig: {
@ -13,10 +14,12 @@ TestConfig: {
}
TestRun: {
random: #Random & {}
suffix: random.#String & {
seed: ""
}
run: docker.#Run & {
name: "daggerci-test-simple-\(random.out)"
name: "daggerci-test-simple-\(suffix.out)"
ref: "hello-world"
ssh: {

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -3,6 +3,7 @@ package main
import (
"dagger.io/docker"
"dagger.io/dagger"
"dagger.io/random"
)
TestConfig: {
@ -11,13 +12,13 @@ TestConfig: {
key: dagger.#Secret @dagger(input)
}
key: dagger.#Secret @dagger(input)
TestSSH: {
random: #Random & {}
suffix: random.#String & {
seed: ""
}
run: docker.#Run & {
name: "daggerci-test-simple-\(random.out)"
name: "daggerci-test-simple-\(suffix.out)"
ref: "hello-world"
ssh: {

View File

@ -4,15 +4,18 @@ import (
"dagger.io/gcp"
"dagger.io/gcp/gcr"
"dagger.io/dagger/op"
"dagger.io/random"
)
TestConfig: gcpConfig: gcp.#Config
TestGCR: {
random: #Random & {}
suffix: random.#String & {
seed: ""
}
repository: "gcr.io/dagger-ci/test"
tag: "test-gcr-\(random.out)"
tag: "test-gcr-\(suffix.out)"
creds: gcr.#Credentials & {
config: TestConfig.gcpConfig
@ -25,7 +28,7 @@ TestGCR: {
op.#DockerBuild & {
dockerfile: """
FROM alpine
RUN echo \(random.out) > /test
RUN echo \(suffix.out) > /test
"""
},
@ -61,7 +64,7 @@ TestGCR: {
op.#Exec & {
always: true
args: [
"sh", "-c", "test $(cat test) = \(random.out)",
"sh", "-c", "test $(cat test) = \(suffix.out)",
]
},
]
@ -76,7 +79,7 @@ TestGCR: {
op.#DockerBuild & {
dockerfile: #"""
FROM \#(push.ref)
RUN test $(cat test) = \#(random.out)
RUN test $(cat test) = \#(suffix.out)
"""#
},
]

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -3,6 +3,7 @@ package main
import (
"dagger.io/dagger"
"dagger.io/kubernetes/helm"
"dagger.io/random"
)
// We assume that a kinD cluster is running locally
@ -11,11 +12,13 @@ kubeconfig: dagger.#Secret @dagger(input)
// Deploy user local chart
TestHelmSimpleChart: {
random: #Random & {}
suffix: random.#String & {
seed: "simple"
}
// Deploy chart
deploy: helm.#Chart & {
name: "dagger-test-helm-simple-chart-\(random.out)"
name: "dagger-test-helm-simple-chart-\(suffix.out)"
namespace: "dagger-test"
"kubeconfig": kubeconfig
chartSource: dagger.#Artifact
@ -30,11 +33,13 @@ TestHelmSimpleChart: {
// Deploy remote chart
TestHelmRepoChart: {
random: #Random & {}
suffix: random.#String & {
seed: "repo"
}
// Deploy chart
deploy: helm.#Chart & {
name: "dagger-test-helm-repository-\(random.out)"
name: "dagger-test-helm-repository-\(suffix.out)"
namespace: "dagger-test"
"kubeconfig": kubeconfig
chart: "redis"

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -4,6 +4,7 @@ import (
"encoding/yaml"
"dagger.io/dagger"
"dagger.io/kubernetes"
"dagger.io/random"
)
// We assume that a kinD cluster is running locally
@ -11,13 +12,15 @@ import (
kubeconfig: dagger.#Secret @dagger(input)
TestKubeApply: {
random: #Random & {}
suffix: random.#String & {
seed: ""
}
// Pod spec
kubeSrc: {
apiVersion: "v1"
kind: "Pod"
metadata: name: "kube-test-\(random.out)"
metadata: name: "kube-test-\(suffix.out)"
spec: {
restartPolicy: "Never"
containers: [{

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}

View File

@ -4,15 +4,18 @@ import (
"dagger.io/dagger/op"
"dagger.io/alpine"
"dagger.io/netlify"
"dagger.io/random"
)
TestNetlify: {
random: #Random & {}
data: random.#String & {
seed: ""
}
// Generate a website containing the random number
html: #up: [
op.#WriteFile & {
content: random.out
content: data.out
dest: "index.html"
},
]
@ -40,7 +43,7 @@ TestNetlify: {
"pipefail",
"-c",
#"""
test "$(curl \#(deploy.deployUrl))" = "\#(random.out)"
test "$(curl \#(deploy.deployUrl))" = "\#(data.out)"
"""#,
]
},

View File

@ -1,49 +0,0 @@
package main
import (
"strconv"
"dagger.io/alpine"
"dagger.io/dagger/op"
)
#Random: {
size: *12 | number
out: {
string
#up: [
op.#Load & {from: alpine.#Image & {
package: python3: "=~3.8"
}},
op.#WriteFile & {
dest: "/entrypoint.py"
content: #"""
import random
import string
import os
size = int(os.environ['SIZE'])
letters = string.ascii_lowercase
print ( ''.join(random.choice(letters) for i in range(size)) )
"""#
},
op.#Exec & {
always: true
args: ["sh", "-c", #"""
printf "$(python3 /entrypoint.py)" > /rand
"""#,
]
env: SIZE: strconv.FormatInt(size, 10)
},
op.#Export & {
source: "/rand"
},
]
}
}