Skip to content
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
39 changes: 39 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
properties(
[
buildDiscarder(
logRotator(
numToKeepStr: '5'
)
)
]
)

node('go1.23') {
container('run'){
def tag = ''
stage('Checkout') {
checkout scm
tag = sh(script: 'git tag -l --contains HEAD', returnStdout: true).trim()
}

stage('Fetch dependencies') {
// using ID because: https://issues.jenkins-ci.org/browse/JENKINS-32101
sshagent(credentials: ['18270936-0906-4c40-a90e-bcf6661f501d']) {
sh('go mod download')
}
}

stage('Run test') {
sh('make test')
}

if (env.BRANCH_NAME == 'main' && tag != '') {
stage('Generate and push docker image'){
docker.withRegistry("https://quay.io", 'docker-registry') {
strippedTag = tag.replaceFirst('v', '')
sh("make push VERSION=${strippedTag}")
}
}
}
}
}
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
.PHONY: test imports
SHELL := /bin/bash

VERSION?=0.0.1-local

IMAGE = quay.io/fortnox/gitmachinecontroller

build:
CGO_ENABLED=0 GOOS=linux go build

docker: build
docker build --pull --rm -t $(IMAGE):$(VERSION) .

push: docker
docker push $(IMAGE):$(VERSION)

test: imports
go test -v ./...

Expand Down
11 changes: 8 additions & 3 deletions cmd/gmc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func app() *cli.App {
Usage: "installs the agent on a new machine.",
Action: func(c *cli.Context) error {
admin := admin.NewAdminFromContext(c)
return admin.Bootstrap(c.Context)
return admin.Bootstrap(c.Context, c.Args().Slice())
},
Flags: []cli.Flag{
&cli.StringFlag{
Expand All @@ -244,11 +244,16 @@ func app() *cli.App {
Usage: "config file location. contains info about the master urls",
},
&cli.StringFlag{
// this is the client for SREs so it needs to have a config somewhere.
Name: "location",
Name: "target-path",
Value: "/usr/local/bin",
Usage: "where to put the gmc binary when bootstrapping",
},
&cli.StringFlag{
Name: "ssh-user",
Value: "",
Aliases: []string{"u"},
Usage: "which user to ssh as",
},
&cli.BoolFlag{
Name: "dry",
Value: false,
Expand Down
52 changes: 0 additions & 52 deletions e2e/httpclient_test.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,12 @@
package e2e_test

import (
"encoding/json"
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

type authedHttpClient struct {
Token string
baseURL string
}

func NewAuthedHttpClient(t *testing.T, baseURL string) *authedHttpClient {
type tokenStruct struct {
Jwt string
}

resp, err := http.Post(baseURL+"/api/admin-v1", "application/json", nil)
assert.NoError(t, err)
defer resp.Body.Close()

token := &tokenStruct{}
err = json.NewDecoder(resp.Body).Decode(token)
assert.NoError(t, err)

return &authedHttpClient{
Token: token.Jwt,
baseURL: baseURL,
}
}

func (ahc *authedHttpClient) Post(u string, body io.Reader) (resp *http.Response, err error) {
u = strings.TrimLeft(u, "/")

req, err := http.NewRequest(http.MethodPost, ahc.baseURL+"/"+u, body)
if err != nil {
return nil, err
}

req.Header.Add("Authorization", ahc.Token)

return http.DefaultClient.Do(req)
}
func (ahc *authedHttpClient) Get(u string) (resp *http.Response, err error) {
u = strings.TrimLeft(u, "/")

req, err := http.NewRequest(http.MethodGet, ahc.baseURL+"/"+u, nil)
if err != nil {
return nil, err
}

req.Header.Add("Authorization", ahc.Token)

return http.DefaultClient.Do(req)
}

func getBody(t *testing.T, b io.ReadCloser) string {

d, err := io.ReadAll(b)
Expand Down
7 changes: 5 additions & 2 deletions e2e/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"github.com/fortnoxab/gitmachinecontroller/mocks"
"github.com/fortnoxab/gitmachinecontroller/pkg/agent"
"github.com/fortnoxab/gitmachinecontroller/pkg/agent/config"
"github.com/fortnoxab/gitmachinecontroller/pkg/authedhttpclient"
"github.com/fortnoxab/gitmachinecontroller/pkg/master"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

type testWrapper struct {
client *authedHttpClient
client *authedhttpclient.Client
master *master.Master
agent *agent.Agent
commander *mocks.MockCommander
Expand Down Expand Up @@ -71,7 +72,9 @@ func initMasterAgent(t *testing.T, ctx context.Context) testWrapper {
}()

time.Sleep(400 * time.Millisecond)
client := NewAuthedHttpClient(t, "http://localhost:"+portStr)
client := authedhttpclient.New(t, "http://localhost:"+portStr)
err = client.AuthAsAdmin()
assert.NoError(t, err)

t.Cleanup(func() {
os.Remove("./agentConfig")
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.10.0
github.com/urfave/cli/v2 v2.27.5
golang.org/x/crypto v0.29.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/apimachinery v0.31.3
)
Expand Down Expand Up @@ -88,9 +89,9 @@ require (
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/arch v0.12.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,11 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
23 changes: 4 additions & 19 deletions pkg/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type Admin struct {
dry bool

// binary location when Bootstrap
location string
targetPath string
sshUser string
}

func NewAdminFromContext(c *cli.Context) *Admin {
Expand All @@ -50,7 +51,8 @@ func NewAdminFromContext(c *cli.Context) *Admin {
selector: c.String("selector"),
regexp: c.String("regexp"),
dry: c.Bool("dry"),
location: c.String("location"),
targetPath: c.String("target-path"),
sshUser: c.String("ssh-user"),
}
}

Expand Down Expand Up @@ -276,23 +278,6 @@ func (a *Admin) Apply(ctx context.Context, args []string) error {
return nil
}

func (a *Admin) Bootstrap(ctx context.Context) error {
conf, err := a.config()
if err != nil {
return err
}

// find where own binary is located
// SCP binary to target
// ssh to target and start binary with --one-shot
// (do we need to wait that it shows up in pending list?)
// call /api/machines/accept-v1 with {"host":hostname} body

// it will configure it-self from git and then die and start itself with systemd.

fmt.Println(conf)
return nil
}
func (a *Admin) Proxy(ctx context.Context) error {
conf, err := a.config()
if err != nil {
Expand Down
Loading