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

many features and improvements #22

Merged
merged 5 commits into from
Jul 2, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .github/workflows/binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
- name: "Build binary"
run: |
sudo apt-get update
sudo apt-get install -y build-essential libvirt-dev make libguestfs-dev
sudo apt-get install -y build-essential libvirt-dev make genisoimage libguestfs-dev libcephfs-dev librbd-dev librados-dev
make

- uses: actions/upload-artifact@v3
with:
name: yavirt-ubuntu
path: bin
path: bin
21 changes: 17 additions & 4 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,27 @@ on:
jobs:
lint:
runs-on: ubuntu-latest
container: projecteru2/footstone:yavirt-prebuild
steps:
- uses: actions/checkout@v3
- name: checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v3
- name: "Setup go"
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'

- name: "Install dependencies"
run: |
sudo apt-get update
sudo apt-get install -y build-essential libvirt-dev make genisoimage libguestfs-dev libcephfs-dev librbd-dev librados-dev

- name: "Install dependencies"
run: |
sudo apt-get update
sudo apt-get install -y build-essential libvirt-dev make genisoimage libguestfs-dev libcephfs-dev librbd-dev librados-dev

- uses: golangci/golangci-lint-action@v3
- uses: golangci/golangci-lint-action@v6
with:
args: --timeout=8m
19 changes: 15 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ on:
jobs:
unittests:
runs-on: ubuntu-latest
container: projecteru2/footstone:yavirt-prebuild

steps:
- uses: actions/checkout@v3
- name: checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/setup-go@v3
- name: "Setup go"
uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'

- name: "Install dependencies"
run: |
sudo apt-get update
sudo apt-get install -y build-essential libvirt-dev make genisoimage libguestfs-dev libcephfs-dev librbd-dev librados-dev
- name: "Install dependencies"
run: |
sudo apt-get update
sudo apt-get install -y build-essential libvirt-dev make genisoimage libguestfs-dev libcephfs-dev librbd-dev librados-dev

- name: unit tests
run: make test
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ cscope.*
vendor/
dist/
yavirt

.vscode
.idea/
.vscode
/tmp
12 changes: 8 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
run:
timeout: 5m
tests: false
skip-dirs:
modules-download-mode: readonly

issues:
exclude-dirs:
- vendor
- tools
- 3rdmocks
modules-download-mode: readonly

- thirdpart
- tmp
- mocks

linters-settings:
nakedret:
max-func-lines: 59
Expand All @@ -27,7 +32,6 @@ linters-settings:
for-loops: true
errcheck:
check-type-assertions: true
check-blank: true
gocritic:
disabled-checks:
- captLocal
Expand Down
25 changes: 19 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
FROM projecteru2/footstone:yavirt-prebuild-go1.20 AS BUILD
FROM ubuntu:jammy AS BUILD

# make binary
# RUN git clone https://github.com/projecteru2/yavirt.git /go/src/github.com/projecteru2/yavirt
COPY . /go/src/github.com/projecteru2/yavirt
WORKDIR /go/src/github.com/projecteru2/yavirt
ARG KEEP_SYMBOL
RUN make deps && make && ./bin/yavirtd --version
RUN sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list
RUN apt update
RUN apt install -y golang-1.20 build-essential libvirt-dev make genisoimage libguestfs-dev libcephfs-dev librbd-dev librados-dev
RUN apt install -y git
# RUN snap install go --classic
ENV PATH="$PATH:/usr/lib/go-1.20/bin/"

FROM alpine:latest
RUN go version
RUN make deps CN=1
RUN make && ./bin/yavirtd --version

FROM ubuntu:jammy

RUN mkdir /etc/yavirt/ && \
sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list && \
apt update && \
apt install -y libvirt-dev libguestfs-dev genisoimage libcephfs-dev librbd-dev librados-dev

RUN mkdir /etc/yavirt/
LABEL ERU=1
COPY --from=BUILD /go/src/github.com/projecteru2/yavirt/bin/yavirtd /usr/bin/yavirtd
COPY --from=BUILD /go/src/github.com/projecteru2/yavirt/bin/yavirtctl /usr/bin/yavirtctl
COPY --from=BUILD /go/src/github.com/projecteru2/yavirt/internal/virt/template/disk.xml /etc/yavirt/disk.xml
COPY --from=BUILD /go/src/github.com/projecteru2/yavirt/internal/virt/template/guest.xml /etc/yavirt/guest.xml
COPY --from=BUILD /go/src/github.com/projecteru2/yavirt/internal/virt/domain/templates/disk.xml /etc/yavirt/disk.xml
COPY --from=BUILD /go/src/github.com/projecteru2/yavirt/internal/virt/domain/templates/guest.xml /etc/yavirt/guest.xml
38 changes: 26 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
ifeq ($(CN), 1)
ENV := GOPROXY=https://goproxy.cn,direct
endif

NS := github.com/projecteru2/yavirt
BUILD := go build -race
TEST := go test -count=1 -race -cover
TEST := go test -count=1 -race -cover -gcflags=all=-l

LDFLAGS += -X "$(NS)/internal/ver.Git=$(shell git rev-parse HEAD)"
LDFLAGS += -X "$(NS)/internal/ver.Compile=$(shell go version)"
LDFLAGS += -X "$(NS)/internal/ver.Date=$(shell date +'%F %T %z')"
REVISION := $(shell git rev-parse HEAD || unknown)
BUILTAT := $(shell date +%Y-%m-%dT%H:%M:%S)
VERSION := $(shell git describe --tags $(shell git rev-list --tags --max-count=1))

PKGS := $$(go list ./... | grep -v -P '$(NS)/third_party|vendor/')
LDFLAGS += -X "$(NS)/internal/ver.REVISION=$(REVISION)"
LDFLAGS += -X "$(NS)/internal/ver.BUILTAT=$(BUILTAT)"
LDFLAGS += -X "$(NS)/internal/ver.VERSION=$(VERSION)"

PKGS := $$(go list ./... | grep -v -P '$(NS)/third_party|vendor/|mocks|ovn')

.PHONY: all test build setup

Expand All @@ -20,12 +28,15 @@ build-srv:
build-ctl:
$(BUILD) -ldflags '$(LDFLAGS)' -o bin/yavirtctl cmd/cmd.go

setup:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/vektra/mockery/v2@latest
setup: setup-lint
$(ENV) go install github.com/vektra/mockery/v2@latest

setup-lint:
$(ENV) go install github.com/golangci/golangci-lint/cmd/[email protected]

lint: format
golangci-lint run --skip-dirs-use-default --skip-dirs=thirdparty
golangci-lint --version
golangci-lint run

format: vet
gofmt -s -w $$(find . -iname '*.go' | grep -v -P '\./third_party|\./vendor/')
Expand All @@ -34,7 +45,8 @@ vet:
go vet $(PKGS)

deps:
go mod tidy
$(ENV) go mod tidy
$(ENV) go mod vendor

mock: deps
mockery --dir pkg/libvirt --output pkg/libvirt/mocks --all
Expand All @@ -43,10 +55,12 @@ mock: deps
mockery --dir pkg/utils --output pkg/utils/mocks --name Locker
mockery --dir internal/virt/agent --output internal/virt/agent/mocks --all
mockery --dir internal/virt/domain --output internal/virt/domain/mocks --name Domain
mockery --dir internal/virt/guest/manager --output internal/virt/guest/manager/mocks --name Manageable
mockery --dir internal/virt/guest --output internal/virt/guest/mocks --name Bot
mockery --dir internal/virt/guestfs --output internal/virt/guestfs/mocks --name Guestfs
mockery --dir internal/virt/volume --output internal/virt/volume/mocks --name Bot
mockery --dir internal/volume --output internal/volume/mocks --name Volume
mockery --dir internal/volume/base --output internal/volume/base/mocks --name SnapshotAPI
mockery --dir internal/eru/store --output internal/eru/store/mocks --name Store
mockery --dir internal/service --output internal/service/mocks --name Service

clean:
rm -fr bin/*
Expand Down
61 changes: 52 additions & 9 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/urfave/cli/v2"

"github.com/cockroachdb/errors"
"github.com/projecteru2/yavirt/cmd/guest"
"github.com/projecteru2/yavirt/cmd/image"
"github.com/projecteru2/yavirt/cmd/maint"
"github.com/projecteru2/yavirt/cmd/network"
"github.com/projecteru2/yavirt/cmd/run"
"github.com/projecteru2/yavirt/configs"
"github.com/projecteru2/yavirt/internal/service/boar"
"github.com/projecteru2/yavirt/internal/ver"
"github.com/projecteru2/yavirt/pkg/errors"
)

func main() {
cli.VersionPrinter = func(c *cli.Context) {
cli.VersionPrinter = func(_ *cli.Context) {
fmt.Println(ver.Version())
}

Expand All @@ -29,7 +32,7 @@ func main() {
},
&cli.StringFlag{
Name: "log-level",
Value: "INFO",
Value: "",
Usage: "set log level",
EnvVars: []string{"ERU_YAVIRT_LOG_LEVEL"},
},
Expand Down Expand Up @@ -57,22 +60,62 @@ func main() {
Usage: "change hostname",
EnvVars: []string{"ERU_HOSTNAME", "HOSTNAME"},
},
&cli.BoolFlag{
Name: "skip-setup-host",
Value: false,
&cli.IntFlag{
Name: "timeout",
Value: 300,
Usage: "command timeout",
EnvVars: []string{"ERU_YAVIRT_CMD_TIMEOUT"},
},
},
Commands: []*cli.Command{
{
Name: "info",
Action: run.Run(info),
},
guest.Command(),
image.Command(),
network.Command(),
maint.Command(),
},

Version: "v",
}

if err := app.Run(os.Args); err != nil {
fmt.Println(errors.Stack(err))
fmt.Println(errors.GetReportableStackTrace(err))
}
}

func info(c *cli.Context, _ run.Runtime) (err error) {
cfg := &configs.Conf

if err := cfg.Load(c.String("config")); err != nil {
return errors.Wrap(err, "")
}
if err := cfg.Prepare(c); err != nil {
return err
}
// disable eru-related features
cfg.Eru.Enable = false

svc, err := boar.New(c.Context, cfg, nil)
if err != nil {
return err
}
info, err := svc.Info()
if err != nil {
return err
}
ans := map[string]string{
"addr": cfg.Host.Addr,
"hostname": cfg.Host.Name,
}
for name, res := range info.Resources {
ans[name] = string(res)
}
b, err := json.MarshalIndent(ans, "", "\t")
if err != nil {
return err
}
fmt.Printf("%s\n", string(b))
return nil
}
Loading
Loading