Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
André Sá committed May 1, 2023
1 parent 556c626 commit bdb211f
Show file tree
Hide file tree
Showing 43 changed files with 2,153 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"

50 changes: 50 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: release

on:
push:
tags:
- "v*"

jobs:
test:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout code
uses: actions/checkout@v3.4.0

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

- name: Install dependencies
run: go mod tidy

- name: Run tests
run: go test -v ./...

release:
needs: [test]
name: Build
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v3.4.0

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

- name: Run GoRelease
uses: goreleaser/goreleaser-action@v4.2.0
with:
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# IDEs
.vscode/
.idea/
27 changes: 27 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
before:
hooks:
- go mod tidy
builds:
- main: ./cmd/main.go
binary: machina
id: machina
env:
- CGO_ENABLED=0
- GITHUB_TOKEN="test"
goarch:
- amd64
ldflags:
- -s
- -w
- -X github.com/enkodr/machina/cmd/machina.version={{.Version}}
- -X github.com/enkodr/machina/cmd/machina.gitCommit={{.Commit}}
- -X github.com/enkodr/machina/cmd/machina.buildDate={{.Date}}
goos:
- linux
archives:
- id: machina
builds:
- machina
format: binary
checksum:
algorithm: sha256
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 enkodr

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
COVER_DIR=/tmp

test:
go test -v -cover ./...

cover:
go test -coverprofile=${COVER_DIR}/coverage.out ./...
go tool cover -html=${COVER_DIR}/coverage.out
41 changes: 41 additions & 0 deletions bin/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/sh

set -eu

# Check if curl is installed
if ! [ -x "$(command -v curl)" ]; then
echo 'Error: curl is not installed' >&2
exit 1
fi

# Check if jq is installed
if ! [ -x "$(command -v curl)" ]; then
echo 'Error: jq is not installed' >&2
exit 1
fi

# Set variables
install_dir="${HOME}/.local/bin"
os="linux"
arch="amd64"

# Check if the install directory exists
if ! [ -d "${install_dir}" ]; then
mkdir -p "${install_dir}"
fi

# Check latest version
latest_release=$(curl -sL https://api.github.com/repos/enkodr/machina/releases/latest | jq -r ".tag_name")
latest_release="${latest_release:1}"

# Download binary
tmp_dir=$(mktemp -d)
curl -s -L https://github.com/enkodr/machina/releases/download/v${latest_release}/machina_${latest_release}_${os}_${arch} -o "${tmp_dir}/machina"
chmod +x "${tmp_dir}/machina"

# Install binary
mv "${tmp_dir}/machina" "${install_dir}/machina"


# Clean up
rm -rf "$tmp_dir"
25 changes: 25 additions & 0 deletions cmd/machina/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package machina

import (
"github.com/spf13/cobra"
)

// Get's the commands and the created machines names for auto-completion
func bashCompleteInstanceNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
// cfg, err := config.New()
// // Get shell completion from cobra
// if err != nil {
// return nil, cobra.ShellCompDirectiveDefault
// }
// // Get instances names for completion
// instances := []string{}
// vms, err := ioutil.ReadDir(cfg.InstancesDirectory)
// if err != nil {
// return nil, cobra.ShellCompDirectiveDefault
// }
// for _, vm := range vms {
// instances = append(instances, vm.Name())
// }
// return instances, cobra.ShellCompDirectiveNoFileComp
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
68 changes: 68 additions & 0 deletions cmd/machina/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package machina

import (
"github.com/enkodr/machina/internal/vm"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// Creates a new VM
var createCommand = &cobra.Command{
Use: "create",
Short: "Creates a new machine",
Aliases: []string{"new"},
Run: func(cmd *cobra.Command, args []string) {
name = "default"
if len(args) == 1 {
name = args[0]
}

if file != "" {
name = file
}
// create a new MachinaVM instance
log.Info("Creating necessary files")
vm, err := vm.NewVM(name)
if err != nil {
log.Fatal(err.Error())
}

// download the image
log.Info("Downloading image")
err = vm.DownloadImage()
if err != nil {
log.Fatal(err.Error())
}

// create disks
log.Info("Create boot disk")
err = vm.CreateDisks()
if err != nil {
log.Fatal(err.Error())
}

// create VM
log.Info("Create and start the VM")
err = vm.Create()
if err != nil {
log.Fatal(err.Error())
}

// wait until is running
log.Info("Waiting for the machine to be ready")
vm.Wait()

// run scritps
log.Info("Running init scripts")
err = vm.RunInitScripts()
if err != nil {
log.Fatal(err.Error())
}

},
}

func init() {
createCommand.PersistentFlags().StringVarP(&file, "file", "f", "", "path to the file to use to create the machine")
rootCommand.AddCommand(createCommand)
}
34 changes: 34 additions & 0 deletions cmd/machina/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package machina

import (
"fmt"

"github.com/enkodr/machina/internal/vm"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var deleteCommand = &cobra.Command{
Use: "delete",
Short: "Delete a machine",
Args: func(cmd *cobra.Command, args []string) error {
return validateName(cmd, args)
},
ValidArgsFunction: namesBashCompletion,
Run: func(cmd *cobra.Command, args []string) {
// Load the machine data
vm, err := vm.Load(name)
if err != nil {
log.Error("the machine doesn't exist")
}

// Start the machine
log.Info(fmt.Sprintf("Deleting machine '%s'", name))
vm.Delete()

},
}

func init() {
rootCommand.AddCommand(deleteCommand)
}
55 changes: 55 additions & 0 deletions cmd/machina/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package machina

import (
"fmt"
"os"
"path/filepath"
"text/tabwriter"

"github.com/enkodr/machina/internal/config"
"github.com/enkodr/machina/internal/vm"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

var listCommand = &cobra.Command{
Use: "list",
Short: "Lists all created machines",
Args: func(cmd *cobra.Command, args []string) error {
return validateName(cmd, args)
},
ValidArgsFunction: namesBashCompletion,
Run: func(cmd *cobra.Command, args []string) {

cfg := config.LoadConfig()
dirs, err := os.ReadDir(cfg.Directories.Instances)
if err != nil {
log.Error("failed to get machines")
}

log.Info("List all machines")
w := tabwriter.NewWriter(os.Stdout, 10, 0, 2, ' ', tabwriter.Debug)
fmt.Fprintln(w, "Name\tIP\tStatus\tCPUs\tMemory\tDisk\tVariant")
for _, dir := range dirs {
data, _ := os.ReadFile(filepath.Join(cfg.Directories.Instances, dir.Name(), "machina.yaml"))
vm := &vm.VMConfig{}
yaml.Unmarshal(data, vm)

fmt.Fprintln(w,
vm.Name,
"\t", vm.Network.IPAddress,
"\t", "",
"\t", vm.Specs.CPUs,
"\t", vm.Specs.Memory,
"\t", vm.Specs.Disk,
"\t", vm.Variant,
)
}
w.Flush()
},
}

func init() {
rootCommand.AddCommand(listCommand)
}
Loading

0 comments on commit bdb211f

Please sign in to comment.