Skip to content

Commit

Permalink
Merge pull request #17 from gabriel-samfira/remove-dependency-on-go-g…
Browse files Browse the repository at this point in the history
…ithub

Remove dependency on go-github
  • Loading branch information
gabriel-samfira authored Oct 9, 2023
2 parents 002b44d + f9b1221 commit d972653
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 76 deletions.
29 changes: 12 additions & 17 deletions cloudconfig/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
"strings"

"github.com/cloudbase/garm-provider-common/defaults"
commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/google/go-github/v55/github"
"github.com/cloudbase/garm-provider-common/params"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -61,7 +60,7 @@ func sortMapKeys(m map[string][]byte) []string {
}

// GetSpecs returns the cloud config specific extra specs from the bootstrap params.
func GetSpecs(bootstrapParams commonParams.BootstrapInstance) (CloudConfigSpec, error) {
func GetSpecs(bootstrapParams params.BootstrapInstance) (CloudConfigSpec, error) {
var extraSpecs CloudConfigSpec
if len(bootstrapParams.ExtraSpecs) == 0 {
return extraSpecs, nil
Expand All @@ -85,28 +84,24 @@ func GetSpecs(bootstrapParams commonParams.BootstrapInstance) (CloudConfigSpec,
// GetRunnerInstallScript returns the runner install script for the given bootstrap params.
// This function will return either the default script for the given OS type or will use the supplied template
// if one is provided.
func GetRunnerInstallScript(bootstrapParams commonParams.BootstrapInstance, tools github.RunnerApplicationDownload, runnerName string) ([]byte, error) {
if tools.Filename == nil {
func GetRunnerInstallScript(bootstrapParams params.BootstrapInstance, tools params.RunnerApplicationDownload, runnerName string) ([]byte, error) {
if tools.GetFilename() == "" {
return nil, fmt.Errorf("missing tools filename")
}

if tools.DownloadURL == nil {
if tools.GetDownloadURL() == "" {
return nil, fmt.Errorf("missing tools download URL")
}

var tempToken string
if tools.TempDownloadToken != nil {
tempToken = *tools.TempDownloadToken
}

tempToken := tools.GetTempDownloadToken()
extraSpecs, err := GetSpecs(bootstrapParams)
if err != nil {
return nil, errors.Wrap(err, "getting specs")
}

installRunnerParams := InstallRunnerParams{
FileName: *tools.Filename,
DownloadURL: *tools.DownloadURL,
FileName: tools.GetFilename(),
DownloadURL: tools.GetDownloadURL(),
TempDownloadToken: tempToken,
MetadataURL: bootstrapParams.MetadataURL,
RunnerUsername: defaults.DefaultUser,
Expand Down Expand Up @@ -137,7 +132,7 @@ func GetRunnerInstallScript(bootstrapParams commonParams.BootstrapInstance, tool
// GetCloudInitConfig returns the cloud-init specific userdata config. This config can be used on most clouds
// for most Linux machines. The install runner script must be generated separately either by GetRunnerInstallScript()
// or some other means.
func GetCloudInitConfig(bootstrapParams commonParams.BootstrapInstance, installScript []byte) (string, error) {
func GetCloudInitConfig(bootstrapParams params.BootstrapInstance, installScript []byte) (string, error) {
extraSpecs, err := GetSpecs(bootstrapParams)
if err != nil {
return "", errors.Wrap(err, "getting specs")
Expand Down Expand Up @@ -188,21 +183,21 @@ func GetCloudInitConfig(bootstrapParams commonParams.BootstrapInstance, installS
// Windows initialization scripts are run by creating a separate CustomScriptExtension resource for each individual script.
// On other clouds it may be different. This function aims to be generic, which is why it only supports the PreInstallScripts
// via cloud-init.
func GetCloudConfig(bootstrapParams commonParams.BootstrapInstance, tools github.RunnerApplicationDownload, runnerName string) (string, error) {
func GetCloudConfig(bootstrapParams params.BootstrapInstance, tools params.RunnerApplicationDownload, runnerName string) (string, error) {
installScript, err := GetRunnerInstallScript(bootstrapParams, tools, runnerName)
if err != nil {
return "", errors.Wrap(err, "generating script")
}

var asStr string
switch bootstrapParams.OSType {
case commonParams.Linux:
case params.Linux:
cloudCfg, err := GetCloudInitConfig(bootstrapParams, installScript)
if err != nil {
return "", errors.Wrap(err, "getting cloud init config")
}
return cloudCfg, nil
case commonParams.Windows:
case params.Windows:
asStr = string(installScript)
default:
return "", fmt.Errorf("unknown os type: %s", bootstrapParams.OSType)
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/cloudbase/garm-provider-common
go 1.20

require (
github.com/google/go-github/v55 v55.0.1-0.20230921135834-aa3fcbe7aabc
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
github.com/mattn/go-isatty v0.0.19
Expand All @@ -20,6 +19,5 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-github/v55 v55.0.1-0.20230921135834-aa3fcbe7aabc h1:wZybOt4gfOPJmwpe3CZFJYoREaqgngGeo1Y29zZePhg=
github.com/google/go-github/v55 v55.0.1-0.20230921135834-aa3fcbe7aabc/go.mod h1:dx9O5B1Z9+WYDRfSIkPdJ/jszShiNtl++jbgL/3OM2c=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
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/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
Expand All @@ -29,7 +23,6 @@ golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98y
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
Expand Down
61 changes: 61 additions & 0 deletions params/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package params

// RunnerApplicationDownload represents a binary for the self-hosted runner application that can be downloaded.
// This is copied from the go-github package. It does not make sense to create a dependency on go-github just
// for this struct.
type RunnerApplicationDownload struct {
OS *string `json:"os,omitempty"`
Architecture *string `json:"architecture,omitempty"`
DownloadURL *string `json:"download_url,omitempty"`
Filename *string `json:"filename,omitempty"`
TempDownloadToken *string `json:"temp_download_token,omitempty"`
SHA256Checksum *string `json:"sha256_checksum,omitempty"`
}

// GetArchitecture returns the Architecture field if it's non-nil, zero value otherwise.
func (r *RunnerApplicationDownload) GetArchitecture() string {
if r == nil || r.Architecture == nil {
return ""
}
return *r.Architecture
}

// GetDownloadURL returns the DownloadURL field if it's non-nil, zero value otherwise.
func (r *RunnerApplicationDownload) GetDownloadURL() string {
if r == nil || r.DownloadURL == nil {
return ""
}
return *r.DownloadURL
}

// GetFilename returns the Filename field if it's non-nil, zero value otherwise.
func (r *RunnerApplicationDownload) GetFilename() string {
if r == nil || r.Filename == nil {
return ""
}
return *r.Filename
}

// GetOS returns the OS field if it's non-nil, zero value otherwise.
func (r *RunnerApplicationDownload) GetOS() string {
if r == nil || r.OS == nil {
return ""
}
return *r.OS
}

// GetSHA256Checksum returns the SHA256Checksum field if it's non-nil, zero value otherwise.
func (r *RunnerApplicationDownload) GetSHA256Checksum() string {
if r == nil || r.SHA256Checksum == nil {
return ""
}
return *r.SHA256Checksum
}

// GetTempDownloadToken returns the TempDownloadToken field if it's non-nil, zero value otherwise.
func (r *RunnerApplicationDownload) GetTempDownloadToken() string {
if r == nil || r.TempDownloadToken == nil {
return ""
}
return *r.TempDownloadToken
}
6 changes: 2 additions & 4 deletions params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package params

import (
"encoding/json"

"github.com/google/go-github/v55/github"
)

type (
Expand Down Expand Up @@ -63,8 +61,8 @@ type UserDataOptions struct {
}

type BootstrapInstance struct {
Name string `json:"name"`
Tools []*github.RunnerApplicationDownload `json:"tools"`
Name string `json:"name"`
Tools []RunnerApplicationDownload `json:"tools"`
// RepoURL is the URL the github runner agent needs to configure itself.
RepoURL string `json:"repo_url"`
// CallbackUrl is the URL where the instance can send a post, signaling
Expand Down
79 changes: 37 additions & 42 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ import (
"unicode/utf16"

runnerErrors "github.com/cloudbase/garm-provider-common/errors"
"github.com/cloudbase/garm-provider-common/params"

commonParams "github.com/cloudbase/garm-provider-common/params"

"github.com/google/go-github/v55/github"
"github.com/google/uuid"
gorillaHandlers "github.com/gorilla/handlers"
"github.com/pkg/errors"
Expand All @@ -50,24 +48,24 @@ const alphanumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
var rxEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")

var (
OSToOSTypeMap map[string]commonParams.OSType = map[string]commonParams.OSType{
"almalinux": commonParams.Linux,
"alma": commonParams.Linux,
"alpine": commonParams.Linux,
"archlinux": commonParams.Linux,
"arch": commonParams.Linux,
"centos": commonParams.Linux,
"ubuntu": commonParams.Linux,
"rhel": commonParams.Linux,
"suse": commonParams.Linux,
"opensuse": commonParams.Linux,
"fedora": commonParams.Linux,
"debian": commonParams.Linux,
"flatcar": commonParams.Linux,
"gentoo": commonParams.Linux,
"rockylinux": commonParams.Linux,
"rocky": commonParams.Linux,
"windows": commonParams.Windows,
OSToOSTypeMap map[string]params.OSType = map[string]params.OSType{
"almalinux": params.Linux,
"alma": params.Linux,
"alpine": params.Linux,
"archlinux": params.Linux,
"arch": params.Linux,
"centos": params.Linux,
"ubuntu": params.Linux,
"rhel": params.Linux,
"suse": params.Linux,
"opensuse": params.Linux,
"fedora": params.Linux,
"debian": params.Linux,
"flatcar": params.Linux,
"gentoo": params.Linux,
"rockylinux": params.Linux,
"rocky": params.Linux,
"windows": params.Windows,
}

githubArchMapping map[string]string = map[string]string{
Expand All @@ -86,9 +84,9 @@ var (
}

//
githubOSTag = map[commonParams.OSType]string{
commonParams.Linux: "Linux",
commonParams.Windows: "Windows",
githubOSTag = map[params.OSType]string{
params.Linux: "Linux",
params.Windows: "Windows",
}
)

Expand Down Expand Up @@ -119,7 +117,7 @@ func ResolveToGithubOSType(osType string) (string, error) {
// ResolveToGithubTag returns the default OS tag that self hosted runners automatically
// (and forcefully) adds to every runner that gets deployed. We need to keep track of those
// tags internally as well.
func ResolveToGithubTag(os commonParams.OSType) (string, error) {
func ResolveToGithubTag(os params.OSType) (string, error) {
ghOS, ok := githubOSTag[os]
if !ok {
return "", runnerErrors.NewNotFoundError("os %s is unknown", os)
Expand Down Expand Up @@ -178,37 +176,34 @@ func ConvertFileToBase64(file string) (string, error) {
return base64.StdEncoding.EncodeToString(bytes), nil
}

func OSToOSType(os string) (commonParams.OSType, error) {
func OSToOSType(os string) (params.OSType, error) {
osType, ok := OSToOSTypeMap[strings.ToLower(os)]
if !ok {
return commonParams.Unknown, fmt.Errorf("no OS to OS type mapping for %s", os)
return params.Unknown, fmt.Errorf("no OS to OS type mapping for %s", os)
}
return osType, nil
}

func GetTools(osType commonParams.OSType, osArch commonParams.OSArch, tools []*github.RunnerApplicationDownload) (github.RunnerApplicationDownload, error) {
func GetTools(osType params.OSType, osArch params.OSArch, tools []params.RunnerApplicationDownload) (params.RunnerApplicationDownload, error) {
// Validate image OS. Linux only for now.
switch osType {
case commonParams.Linux:
case commonParams.Windows:
case params.Linux:
case params.Windows:
default:
return github.RunnerApplicationDownload{}, fmt.Errorf("unsupported OS type: %s", osType)
return params.RunnerApplicationDownload{}, fmt.Errorf("unsupported OS type: %s", osType)
}

switch osArch {
case commonParams.Amd64:
case commonParams.Arm:
case commonParams.Arm64:
case params.Amd64:
case params.Arm:
case params.Arm64:
default:
return github.RunnerApplicationDownload{}, fmt.Errorf("unsupported OS arch: %s", osArch)
return params.RunnerApplicationDownload{}, fmt.Errorf("unsupported OS arch: %s", osArch)
}

// Find tools for OS/Arch.
for _, tool := range tools {
if tool == nil {
continue
}
if tool.OS == nil || tool.Architecture == nil {
if tool.GetOS() == "" || tool.GetArchitecture() == "" {
continue
}

Expand All @@ -221,11 +216,11 @@ func GetTools(osType commonParams.OSType, osArch commonParams.OSArch, tools []*g
if err != nil {
continue
}
if *tool.Architecture == ghArch && *tool.OS == ghOS {
return *tool, nil
if tool.GetArchitecture() == ghArch && tool.GetOS() == ghOS {
return tool, nil
}
}
return github.RunnerApplicationDownload{}, fmt.Errorf("failed to find tools for OS %s and arch %s", osType, osArch)
return params.RunnerApplicationDownload{}, fmt.Errorf("failed to find tools for OS %s and arch %s", osType, osArch)
}

// GetRandomString returns a secure random string
Expand Down
7 changes: 3 additions & 4 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

runnerErrors "github.com/cloudbase/garm-provider-common/errors"
"github.com/cloudbase/garm-provider-common/params"
"github.com/google/go-github/v55/github"
"github.com/stretchr/testify/require"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
Expand Down Expand Up @@ -211,10 +210,10 @@ func TestGetTools(t *testing.T) {
t.Fatalf("failed to resolve to github os type: %s", err)
}

tools := []*github.RunnerApplicationDownload{
tools := []params.RunnerApplicationDownload{
{
OS: github.String(ghOS),
Architecture: github.String(ghArch),
OS: &ghOS,
Architecture: &ghArch,
},
}

Expand Down

0 comments on commit d972653

Please sign in to comment.