Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template functions for userdata #104

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions drivers/internal/userdata/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package userdata
import (
"encoding/base64"
"text/template"

"github.com/drone/funcmap"
)

var funcs = map[string]interface{}{
Expand All @@ -19,12 +21,12 @@ var funcs = map[string]interface{}{
func Parse(text string) *template.Template {
if decoded, err := base64.StdEncoding.DecodeString(text); err == nil {
return template.Must(
template.New("_").Funcs(funcs).Parse(string(decoded)),
template.New("_").Funcs(funcs).Funcs(funcmap.Funcs).Parse(string(decoded)),
)
}

return template.Must(
template.New("_").Funcs(funcs).Parse(text),
template.New("_").Funcs(funcs).Funcs(funcmap.Funcs).Parse(text),
)
}

Expand Down
39 changes: 39 additions & 0 deletions drivers/internal/userdata/userdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package userdata

import (
"bytes"
"fmt"
"testing"

"github.com/drone/autoscaler"
Expand All @@ -25,6 +26,21 @@ func TestUserdata(t *testing.T) {
}
}

func TestUserdataFuncmap(t *testing.T) {
buf := new(bytes.Buffer)
err := UD.Execute(buf, &map[string]interface{}{
"Content": "foo",
})
fmt.Println(buf.String())
if err != nil {
t.Error(err)
return
}
if buf.String() != UDExpected {
t.Errorf("expected '%s', got '%s'", UDExpected, buf.String())
}
}

var dummyCA = `-----BEGIN CERTIFICATE-----
MIIGOTCCBCGgAwIBAgIJAOE/vJd8EB24MA0GCSqGSIb3DQEBBQUAMIGyMQswCQYD
VQQGEwJGUjEPMA0GA1UECAwGQWxzYWNlMRMwEQYDVQQHDApTdHJhc2JvdXJnMRgw
Expand All @@ -48,3 +64,26 @@ bGFuIFNhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkxIjAgBgkqhkiG9w0BCQEW
E2NvbnRhY3RAZnJlZWxhbi5vcmcwHhcNMTIwNDI3MTAzMTE4WhcNMjIwNDI1MTAz
DiH5uEqBXExjrj0FslxcVKdVj5glVcSmkLwZKbEU1OKwleT/iXFhvooWhQ==
-----END CERTIFICATE-----`

var UD = Parse(`#cloud-config

apt_reboot_if_required:
package_update: false
package_upgrade: false

write_files:
- path: /etc/systemd/system/docker.service.d/override.conf
content: | {{nindent .Content 6 }}
`)

var UDExpected = `#cloud-config

apt_reboot_if_required:
package_update: false
package_upgrade: false

write_files:
- path: /etc/systemd/system/docker.service.d/override.conf
content: |
foo
`
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ require (
cloud.google.com/go v0.28.0 // indirect
github.com/99designs/basicauth-go v0.0.0-20160802081356-2a93ba0f464d
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/Microsoft/go-winio v0.4.7 // indirect
github.com/aws/aws-sdk-go v1.13.5
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a // indirect
Expand All @@ -21,6 +24,7 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/drone/drone-go v1.0.5-0.20190504210458-4d6116b897ba
github.com/drone/envconfig v1.4.1
github.com/drone/funcmap v0.0.0-20210903193859-704120d6923c
github.com/drone/signal v0.0.0-20170915013802-ac5d07ef1315
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4
github.com/go-chi/chi v3.3.2+incompatible
Expand All @@ -30,10 +34,13 @@ require (
github.com/golang/mock v1.3.1
github.com/google/go-cmp v0.4.0
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gophercloud/gophercloud v0.0.0-20181014043407-c8947f7d1c51
github.com/gorilla/mux v1.7.4 // indirect
github.com/h2non/gock v1.0.7
github.com/hetznercloud/hcloud-go v1.4.0
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 // indirect
github.com/jmoiron/sqlx v0.0.0-20180228184624-cf35089a1979
github.com/joho/godotenv v1.2.0
Expand All @@ -42,6 +49,7 @@ require (
github.com/lib/pq v1.0.0
github.com/mattn/go-sqlite3 v1.6.0
github.com/matttproud/golang_protobuf_extensions v1.0.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
Expand All @@ -68,3 +76,5 @@ require (
gotest.tools v2.2.0+incompatible // indirect
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc // indirect
)

replace github.com/drone/funcmap => github.com/iainlane/funcmap v0.0.0-20211116113722-13f662008062
bradrydzewski marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ github.com/99designs/httpsignatures-go v0.0.0-20170731043157-88528bf4ca7e/go.mod
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/Microsoft/go-winio v0.4.7 h1:vOvDiY/F1avSWlCWiKJjdYKz2jVjTK3pWPHndeG4OAY=
github.com/Microsoft/go-winio v0.4.7/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/aws/aws-sdk-go v1.13.5 h1:OomNUmMMCy/wClWVp5tl7FFMF+TmrjMksbmSuPziFj8=
Expand Down Expand Up @@ -68,6 +74,8 @@ github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 h1:zLTLjkaOFEFIOxY5BWLFLwh+cL8vOBW4XJ2aqLE/Tf0=
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gophercloud/gophercloud v0.0.0-20181014043407-c8947f7d1c51 h1:boFYpbhy0scteX2LHVBgOwPfEMaisiv6ShNqvmi16+I=
github.com/gophercloud/gophercloud v0.0.0-20181014043407-c8947f7d1c51/go.mod h1:3WdhXV3rUYy9p6AUW8d94kr+HS62Y4VL9mBnFxsD8q4=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
Expand All @@ -78,6 +86,12 @@ github.com/h2non/gock v1.0.7 h1:ysCe8XR0rX0kZJJgJgkXN9oz9/Ja5tFA1s+mClGBE0M=
github.com/h2non/gock v1.0.7/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
github.com/hetznercloud/hcloud-go v1.4.0 h1:TXLSp+Ft0jBqbL9CVfENSc+ynjkF+e0xoRko9CVpX9Y=
github.com/hetznercloud/hcloud-go v1.4.0/go.mod h1:g5pff0YNAZywQaivY/CmhUYFVp7oP0nu3MiODC2W4Hw=
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/iainlane/funcmap v0.0.0-20211116113722-13f662008062 h1:Aq01+JaEgPkf7ZJBLxsYctbtWwLv9YxEUngvACcnFC0=
github.com/iainlane/funcmap v0.0.0-20211116113722-13f662008062/go.mod h1:Hph0/pT6ZxbujnE1Z6/08p5I0XXuOsppqF6NQlGOK0E=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Aowv3l/EQUlocDHW2Cp4G9WJVH7uyH8QFJE=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmoiron/sqlx v0.0.0-20180228184624-cf35089a1979 h1:2Xvj9kCxHDj//km9z+jV09L1ATggC+0pDMjqqAfyWcY=
Expand All @@ -98,6 +112,10 @@ github.com/mattn/go-sqlite3 v1.6.0 h1:TDwTWbeII+88Qy55nWlof0DclgAtI4LqGujkYMzmQI
github.com/mattn/go-sqlite3 v1.6.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.0 h1:YNOwxxSJzSUARoD9KRZLzM9Y858MNGCOACTvCW9TSAc=
github.com/matttproud/golang_protobuf_extensions v1.0.0/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
Expand Down Expand Up @@ -191,6 +209,8 @@ gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down