Skip to content

Commit

Permalink
add verbose flag, deprecate debug false
Browse files Browse the repository at this point in the history
Dockerfile: add missing ca-certificates
run: hetzner: add token env var
fix examples

Signed-off-by: Adphi <[email protected]>
  • Loading branch information
Adphi committed Sep 10, 2022
1 parent 1721146 commit 96026b8
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 37 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ qemu.sh
bin
dist
images
examples/build
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ dist/
images
wstation
/d2vm
/examples/build
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ FROM ubuntu:20.04

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \
ca-certificates \
util-linux \
udev \
parted \
Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ build: $(BIN) bin
.PHONY: release
release: $(BIN) bin
@VERSION=$(VERSION) IMAGE=$(DOCKER_IMAGE) goreleaser release --rm-dist --parallelism 8

.PHONY: examples
examples: build-dev
@mkdir -p examples/build
@for f in $$(find examples -type f -name '*Dockerfile' -maxdepth 1); do \
echo "Building $$f"; \
./d2vm build -o examples/build/$$(basename $$f|cut -d'.' -f1).qcow2 -f $$f examples; \
done
@echo "Building examples/full/Dockerfile"
@./d2vm build -o examples/build/full.qcow2 --build-arg=USER=adphi --build-arg=PASSWORD=adphi examples/full
3 changes: 0 additions & 3 deletions cmd/d2vm/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"go.linka.cloud/d2vm"
"go.linka.cloud/d2vm/pkg/docker"
"go.linka.cloud/d2vm/pkg/exec"
)

var (
Expand All @@ -47,7 +46,6 @@ var (
if err != nil {
return err
}
exec.SetDebug(debug)
if file == "" {
file = filepath.Join(args[0], "Dockerfile")
}
Expand All @@ -69,7 +67,6 @@ func init() {
buildCmd.Flags().StringVarP(&output, "output", "o", output, "The output image, the extension determine the image format, raw will be used if none. Supported formats: "+strings.Join(d2vm.OutputFormats(), " "))
buildCmd.Flags().StringVarP(&password, "password", "p", "root", "Root user password")
buildCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size")
buildCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
buildCmd.Flags().BoolVar(&force, "force", false, "Override output image")
buildCmd.Flags().StringVar(&cmdLineExtra, "append-to-cmdline", "", "Extra kernel cmdline arguments to append to the generated one")
buildCmd.Flags().StringVar(&networkManager, "network-manager", "", "Network manager to use for the image: none, netplan, ifupdown")
Expand Down
3 changes: 0 additions & 3 deletions cmd/d2vm/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"go.linka.cloud/d2vm"
"go.linka.cloud/d2vm/pkg/docker"
"go.linka.cloud/d2vm/pkg/exec"
)

var (
Expand Down Expand Up @@ -56,7 +55,6 @@ var (
return fmt.Errorf("%s already exists", output)
}
}
exec.SetDebug(debug)
if _, err := os.Stat(output); err == nil || !os.IsNotExist(err) {
if !force {
return fmt.Errorf("%s already exists", output)
Expand Down Expand Up @@ -97,7 +95,6 @@ func init() {
convertCmd.Flags().StringVarP(&output, "output", "o", output, "The output image, the extension determine the image format, raw will be used if none. Supported formats: "+strings.Join(d2vm.OutputFormats(), " "))
convertCmd.Flags().StringVarP(&password, "password", "p", "root", "The Root user password")
convertCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size")
convertCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
convertCmd.Flags().BoolVarP(&force, "force", "f", false, "Override output qcow2 image")
convertCmd.Flags().StringVar(&cmdLineExtra, "append-to-cmdline", "", "Extra kernel cmdline arguments to append to the generated one")
convertCmd.Flags().StringVar(&networkManager, "network-manager", "", "Network manager to use for the image: none, netplan, ifupdown")
Expand Down
16 changes: 15 additions & 1 deletion cmd/d2vm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,31 @@ import (
"os"
"os/signal"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"go.linka.cloud/d2vm"
"go.linka.cloud/d2vm/pkg/exec"
)

var (
output = "disk0.qcow2"
size = "1G"
password = "root"
force = false
debug = false
verbose = false
format = "qcow2"

rootCmd = &cobra.Command{
Use: "d2vm",
SilenceUsage: true,
Version: d2vm.Version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if verbose {
logrus.SetLevel(logrus.TraceLevel)
}
exec.SetDebug(verbose)
},
}
)

Expand All @@ -53,3 +61,9 @@ func main() {
}()
rootCmd.ExecuteContext(ctx)
}

func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "debug", "d", false, "Enable Debug output")
rootCmd.PersistentFlags().MarkDeprecated("debug", "use -v instead")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable Verbose output")
}
7 changes: 0 additions & 7 deletions cmd/d2vm/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"go.linka.cloud/d2vm/cmd/d2vm/run"
Expand All @@ -25,11 +24,6 @@ var (
runCmd = &cobra.Command{
Use: "run",
Short: "run the converted virtual machine",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
},
}
)

Expand All @@ -39,5 +33,4 @@ func init() {
runCmd.AddCommand(run.VboxCmd)
runCmd.AddCommand(run.QemuCmd)
runCmd.AddCommand(run.HetznerCmd)
runCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
}
13 changes: 8 additions & 5 deletions cmd/d2vm/run/hetzner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import (
)

const (
serverImg = "ubuntu-20.04"
vmBlockPath = "/dev/sda"
sparsecatPath = "/usr/local/bin/sparsecat"
hetznerTokenEnv = "HETZNER_TOKEN"
serverImg = "ubuntu-20.04"
vmBlockPath = "/dev/sda"
sparsecatPath = "/usr/local/bin/sparsecat"
)

var (
Expand All @@ -56,7 +57,7 @@ var (
)

func init() {
HetznerCmd.Flags().StringVarP(&hetznerToken, "token", "t", "", "Hetzner Cloud API token")
HetznerCmd.Flags().StringVarP(&hetznerToken, "token", "t", "", "Hetzner Cloud API token [$"+hetznerTokenEnv+"]")
HetznerCmd.Flags().StringVarP(&hetznerSSHUser, "user", "u", "root", "d2vm image ssh user")
HetznerCmd.Flags().StringVarP(&hetznerSSHKeyPath, "ssh-key", "i", "", "d2vm image identity key")
HetznerCmd.Flags().BoolVar(&hetznerRemove, "rm", false, "remove server when done")
Expand Down Expand Up @@ -85,7 +86,7 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
}
defer src.Close()

c := hcloud.NewClient(hcloud.WithToken(hetznerToken))
c := hcloud.NewClient(hcloud.WithToken(GetStringValue(hetznerTokenEnv, hetznerToken, "")))
st, _, err := c.ServerType.GetByName(ctx, hetznerVMType)
if err != nil {
return err
Expand Down Expand Up @@ -205,8 +206,10 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
logrus.Infof("%s / %d%% transfered ( %s/s)", humanize.Bytes(uint64(b)), int(float64(b)/float64(i.VirtualSize)*100), humanize.Bytes(uint64(b-last)))
last = b
case <-ctx.Done():
logrus.Warnf("context cancelled")
return
case <-done:
logrus.Infof("transfer finished")
return
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/alpine.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM alpine

RUN apk add --no-cache openssh-server && \
RUN apk add --no-cache openrc openssh-server && \
rc-update add sshd default && \
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
3 changes: 2 additions & 1 deletion examples/debian.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM debian

RUN apt update && apt install -y openssh-server && \
RUN apt update && apt install -y openssh-server systemctl && \
systemctl enable ssh && \
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
10 changes: 0 additions & 10 deletions examples/full/00-netconf.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions examples/full/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ FROM ubuntu
# Install some system packages
RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y --no-install-recommends \
qemu-guest-agent \
netplan.io \
ca-certificates \
dnsutils \
sudo \
openssh-server

# Setup default network config
COPY 00-netconf.yaml /etc/netplan/
# Add a utility script to resize serial terminal
COPY resize /usr/local/bin/

Expand Down
3 changes: 2 additions & 1 deletion examples/ubuntu.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM ubuntu

RUN apt update && apt install -y openssh-server && \
RUN apt update && apt install -y openssh-server systemctl && \
systemctl enable ssh && \
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
2 changes: 1 addition & 1 deletion templates/debian.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
systemd \
dbus \
iproute2 \
udhcpc \
isc-dhcp-client \
iputils-ping

RUN systemctl preset-all
Expand Down
2 changes: 1 addition & 1 deletion templates/ubuntu.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN apt-get update -y && \
systemd-sysv \
systemd \
dbus \
udhcpc \
isc-dhcp-client \
iproute2 \
iputils-ping

Expand Down

0 comments on commit 96026b8

Please sign in to comment.