Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"golang.org/x/sync/semaphore"
)
func main() {
flag.Parse()
cc := new(credhubClient)
fc := newFlyClient(flag.Arg(0))
err := setPipeline(cc, fc, 50)
log.Println(err)
}
func setPipeline(cc *credhubClient, fc *flyClient, n int64) error {
const parralelism = 10
const pipeline = `
resources:
- name: 1h
type: time
check_every: 10s
source:
interval: 1h
dummy:
- ((mySecret0))
- ((mySecret1))
- ((mySecret2))
- ((mySecret3))
- ((mySecret4))
- ((mySecret5))
- ((mySecret6))
- ((mySecret7))
- ((mySecret8))
- ((mySecret9))
jobs:
- name: get1h
plan:
- get: 1h
trigger: true
`
s := semaphore.NewWeighted(parralelism)
for i := int64(0); i < n; i++ {
s.Acquire(context.Background(), 1)
go func(i int64) {
defer s.Release(1)
for j := 0; j < 10; j++ {
name := fmt.Sprintf("/concourse/main/test-pipeline-%d/mySecret%d", i, j)
err := cc.set(name, "verySecret")
if err != nil {
log.Println(err)
return
}
}
err := fc.setPipeline(fmt.Sprintf("test-pipeline-%d", i), pipeline)
if err != nil {
log.Println(err)
}
}(i)
}
s.Acquire(context.Background(), parralelism)
return nil
}
type flyClient struct {
target string
}
func newFlyClient(target string) *flyClient {
return &flyClient{
target: target,
}
}
func (c *flyClient) setPipeline(name, conf string) error {
fName, err := writeTempFile([]byte(conf))
if err != nil {
return err
}
defer os.Remove(fName)
err = c.run("set-pipeline", "-n", "-p", name, "-c", fName)
if err != nil {
return err
}
return c.run("unpause-pipeline", "-p", name)
}
func (c *flyClient) run(args ...string) error {
args = append([]string{"--target", c.target}, args...)
_, err := exec.Command("fly", args...).Output()
return exitErr(err)
}
type credhubClient struct{}
func (c *credhubClient) set(name, value string) error {
return c.run("set", "--name", name, "--type=value", "--value", value)
}
func (c *credhubClient) run(args ...string) error {
_, err := exec.Command("credhub", args...).Output()
return exitErr(err)
}
func exitErr(err error) error {
if exitErr, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("%v:\n%s", exitErr.Error(), string(exitErr.Stderr))
}
return err
}
func writeTempFile(data []byte) (string, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
return "", err
}
_, err = f.Write(data)
if err != nil {
f.Close()
os.Remove(f.Name())
return "", err
}
err = f.Close()
if err != nil {
os.Remove(f.Name())
return "", err
}
return f.Name(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.