Skip to content

Commit

Permalink
feat: refactor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-viles committed Oct 2, 2023
1 parent dfac30f commit 59d18c5
Show file tree
Hide file tree
Showing 69 changed files with 3,097 additions and 1,821 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
with:
version: latest
skip-cache: true
args: --timeout=30m
test:
name: Run tests
runs-on: 'ubuntu-22.04'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
with:
version: latest
skip-cache: true
args: --timeout=30m
test:
name: Run tests
runs-on: 'ubuntu-22.04'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
with:
version: latest
skip-cache: true
args: --timeout=30m

test:
name: Run tests
Expand Down
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# Changelog

## [ Upcoming Release - v0.1.0-beta.9 ]
## [ Upcoming Release - v0.1.0-beta.10 ]
### Changed/Added
### Fixed
### Deprecated

## [ 02/08/2023 - v0.1.0-beta.9 ]

### Changed/Added
* Added a server so baski can serve up scans via an API
* Added initial tests
* Added unit-tests
* Updated builds to include server as a separate binary and docker image
* Updated golang version in pipeline
* Removed the publish command and all related code.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package main
import (
"context"
"errors"
"github.com/eschercloudai/baski/pkg/cmd/util/flags"
"github.com/eschercloudai/baski/pkg/server/server"
"github.com/eschercloudai/baski/pkg/util/flags"
"github.com/spf13/cobra"
"log"
"net/http"
Expand Down
6 changes: 3 additions & 3 deletions docker/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ RUN apk add bash build-base jq curl git python3 py3-pip openssh-client
RUN echo "ServerAliveInterval 120" >> /etc/ssh/ssh_config

RUN echo "baski:x:1000:1000:Baski Non Root,,,:/home/baski:" >> /etc/passwd
RUN mkdir -p /home/baski/.config/openstack/
RUN mkdir -p /home/baski/
RUN chown baski: -R /home/baski

COPY --from=go-builder /tmp/baski /usr/bin/
COPY --from=go-builder /tmp/baski-server /usr/bin/

RUN chmod 755 /usr/bin/baski
RUN chmod 755 /usr/bin/baski-server
ENV PATH=${PATH}:/home/baski/.local/bin/

USER 1000
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.3.0
golang.org/x/crypto v0.13.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.1.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
Expand Down
4 changes: 2 additions & 2 deletions hack/check_license/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func glob(extension string) ([]string, error) {
// level (because autogenerated code does what it likes).
func checkGoLicenseInComments(path string, file *ast.File) error {
if len(file.Comments) == 0 {
return fmt.Errorf("%s: %w", path, errNoComments)
return fmt.Errorf("%s: %w\n", path, errNoComments)
}

for _, comment := range file.Comments {
Expand All @@ -105,7 +105,7 @@ func checkGoLicenseInComments(path string, file *ast.File) error {
}
}

return fmt.Errorf("%s: %w", path, errFirstCommentNotLicense)
return fmt.Errorf("%s: %w\n", path, errFirstCommentNotLicense)
}

// checkGoLicenseFile parses a source file and checks there is a license header in there.
Expand Down
32 changes: 19 additions & 13 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package build

import (
"fmt"
"github.com/eschercloudai/baski/pkg/cmd/util/data"
"github.com/eschercloudai/baski/pkg/cmd/util/flags"
"github.com/eschercloudai/baski/pkg/constants"
ostack "github.com/eschercloudai/baski/pkg/openstack"
ostack "github.com/eschercloudai/baski/pkg/providers/openstack"
"github.com/eschercloudai/baski/pkg/providers/packer"
"github.com/eschercloudai/baski/pkg/util/data"
"github.com/eschercloudai/baski/pkg/util/flags"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

Expand Down Expand Up @@ -50,28 +52,32 @@ To use baski to build an image, an Openstack cluster is required.`,
RunE: func(cmd *cobra.Command, args []string) error {
o.SetOptionsFromViper()

cloudsConfig := ostack.InitOpenstack(o.CloudsPath)
packerBuildConfig := ostack.InitPackerConfig(o)
if !checkValidOSSelected(o.BuildOS) {
return fmt.Errorf("an unsupported OS has been entered. Please select a valid OS: %s\n", constants.SupportedOS)
}

buildGitDir := CreateRepoDirectory()
FetchBuildRepo(buildGitDir, o)
err := os.Setenv("OS_CLOUD", o.CloudName)
if err != nil {
return err
}

packerBuildConfig := packer.InitConfig(o)
metadata := ostack.GenerateBuilderMetadata(o)
err := ostack.UpdatePackerBuildersJson(buildGitDir, metadata)

buildGitDir := createRepoDirectory()
fetchBuildRepo(buildGitDir, o)

err = packer.UpdatePackerBuildersJson(buildGitDir, metadata)
if err != nil {
return err
}

capiPath := filepath.Join(buildGitDir, "images", "capi")
packerBuildConfig.GenerateVariablesFile(capiPath)

InstallDependencies(capiPath, o.Verbose)

cloudsConfig.SetOpenstackEnvs(o.CloudName)
installDependencies(capiPath, o.Verbose)

err = BuildImage(capiPath, o.BuildOS, o.Verbose)
err = buildImage(capiPath, o.BuildOS, o.Verbose)
if err != nil {
return err
}
Expand All @@ -81,7 +87,7 @@ To use baski to build an image, an Openstack cluster is required.`,
return err
}

err = SaveImageIDToFile(imgID)
err = saveImageIDToFile(imgID)
if err != nil {
return err
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/cmd/build/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import (
"path/filepath"
"strings"

"github.com/eschercloudai/baski/pkg/cmd/util/flags"
gitRepo "github.com/eschercloudai/baski/pkg/git"
systemUtils "github.com/eschercloudai/baski/pkg/system"
"github.com/eschercloudai/baski/pkg/util/flags"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/uuid"
)

// CreateRepoDirectory create the random directory where the Image repo will be cloned into.
func CreateRepoDirectory() string {
// createRepoDirectory create the random directory where the Image repo will be cloned into.
func createRepoDirectory() string {
var tmpDir string
uuidDir, err := uuid.NewRandom()
if err != nil {
Expand All @@ -48,8 +48,8 @@ func CreateRepoDirectory() string {
return dir
}

// FetchBuildRepo simply pulls the contents of the imageRepo to the specified path
func FetchBuildRepo(path string, o *flags.BuildOptions) {
// fetchBuildRepo simply pulls the contents of the imageRepo to the specified path
func fetchBuildRepo(path string, o *flags.BuildOptions) {
branch := plumbing.ReferenceName("refs/heads/" + o.ImageRepoBranch)
imageRepo := o.ImageRepo

Expand All @@ -67,9 +67,9 @@ func FetchBuildRepo(path string, o *flags.BuildOptions) {
}
}

// InstallDependencies will run make dep-openstack so that any requirements such as packer, ansible
// installDependencies will run make dep-openstack so that any requirements such as packer, ansible
// and goss will be installed.
func InstallDependencies(repoPath string, verbose bool) {
func installDependencies(repoPath string, verbose bool) {
log.Printf("fetching dependencies\n")

w, err := os.Create("/tmp/out-deps.txt")
Expand Down Expand Up @@ -98,9 +98,9 @@ func InstallDependencies(repoPath string, verbose bool) {
}
}

// BuildImage will run make build-openstack-buildOS which will launch an instance in Openstack,
// buildImage will run make build-openstack-buildOS which will launch an instance in Openstack,
// add any requirements as defined in the image-builder imageRepo and then create an image from that build.
func BuildImage(capiPath string, buildOS string, verbose bool) error {
func buildImage(capiPath string, buildOS string, verbose bool) error {
log.Printf("building image\n")

w, err := os.Create("/tmp/out-build.txt")
Expand Down Expand Up @@ -128,8 +128,8 @@ func BuildImage(capiPath string, buildOS string, verbose bool) error {
return nil
}

// SaveImageIDToFile exports the image ID to a file so that it can be read later by the scan system - this will generally be used by the gitHub action.
func SaveImageIDToFile(imgID string) error {
// saveImageIDToFile exports the image ID to a file so that it can be read later by the scan system - this will generally be used by the gitHub action.
func saveImageIDToFile(imgID string) error {
f, err := os.Create("/tmp/imgid.out")
if err != nil {
return err
Expand Down
Loading

0 comments on commit 59d18c5

Please sign in to comment.