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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Powerappstream Builder
Powerappstream builder is a builder agent developed for AWS Appstream image builders. The Appstream imagebuilders lack the ability to build appstream images declaratively. ie via code. Powerappstream builder is one of a tools from powerappstream suite that solves the core issue.
32 changes: 19 additions & 13 deletions cmd/powerappstream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"

"github.com/aslamcodes/powerappstream-builder/internal/agent"
"github.com/aslamcodes/powerappstream-builder/internal/backend"
)

Expand All @@ -16,11 +15,13 @@ func main() {

flag.Parse()

run(*source, *location, os.Stdout)
if err := run(*source, *location, os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func run(sourceType string, location string, out io.Writer) {
agent := agent.Agent{}
func run(sourceType string, location string, out io.Writer) error {
switch sourceType {
case "local":
backend := backend.LocalBackend{
Expand All @@ -30,17 +31,22 @@ func run(sourceType string, location string, out io.Writer) {
config, err := backend.GetConfig()

if err != nil {
fmt.Fprintln(out, err)
return err
}

agent.HandleConfig(config, os.Stdout)
case "powerappstream":
case "s3":
case "git":
case "consul":
case "http":
case "k8":
case "azurerm":
err = config.Setup(out)

if err != nil {
return err
}

// case "powerappstream":
// case "s3":
// case "git":

default:
return fmt.Errorf("Invalid source provided")
}

return nil
}
15 changes: 0 additions & 15 deletions internal/agent/agent.go

This file was deleted.

7 changes: 6 additions & 1 deletion internal/backend/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ func TestGetConfig(t *testing.T) {
},
{
Executable: "powershell",
InstallScript: "echo \"Setting up environment\"\napt-get update\napt-get install -y curl\n",
InstallScript: "echo \"Setting up environment\"\n",
},
{
Executable: "cmd.exe",
InstallScript: "echo hello world\n",
},
},
}

if !reflect.DeepEqual(expected, *actual) {
t.Fatalf("GetConfig() mismatch.\nexpected: %#v\nactual: %#v", expected, *actual)
fmt.Fprintln(os.Stderr, "The expected and actual config are not equal")
t.FailNow()
}
Expand Down
22 changes: 11 additions & 11 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package config

import (
"fmt"
"io"
)

type Installer struct {
InstallScript string `yaml:"installScript"`
Executable string `yaml:"executable"`
}
import "io"

type Config struct {
Installers []Installer `yaml:"installers"`
}

func (i *Installer) Install(out io.Writer) {
fmt.Fprintln(out, fmt.Sprintf("%s %s", i.Executable, i.InstallScript))
func (c *Config) Setup(out io.Writer) error {
for _, i := range c.Installers {
err := i.Install(out)

if err != nil {
return err
}
}

return nil
}
14 changes: 14 additions & 0 deletions internal/config/installer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package config

import (
"io"
)

type Installer struct {
InstallScript string `yaml:"installScript"`
Executable string `yaml:"executable"`
}

func (i *Installer) Install(out io.Writer) error {
return PlatformInstaller(i, out)
}
14 changes: 14 additions & 0 deletions internal/config/installer_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !windows

package config

import (
"fmt"
"io"
)

func PlatformInstaller(installer *Installer, out io.Writer) error {
fmt.Fprintf(out, "unix in progress 🦔")

return nil
}
45 changes: 45 additions & 0 deletions internal/config/installer_win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build windows

package config

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
)

// This piece of code is generated by chatgpt
func PlatformInstaller(inst *Installer, out io.Writer) error {
switch inst.Executable {
case "powershell":
return runScript("powershell.exe", ".ps1", inst.InstallScript, out,
"-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File")
case "cmd.exe":
return runScript("cmd.exe", ".bat", inst.InstallScript, out, "/C")
default:
return fmt.Errorf("unsupported executable: %s", inst.Executable)
}
}

func runScript(exe, ext, body string, out io.Writer, argsPrefix ...string) error {
f := filepath.Join(os.TempDir(), "installer"+ext)
if err := os.WriteFile(f, []byte(body), 0600); err != nil {
return err
}

args := append(argsPrefix, f)

cmd := exec.Command(exe, args...)

output, err := cmd.CombinedOutput()

if err != nil {
return err
}

out.Write(output)

return nil
}
7 changes: 5 additions & 2 deletions testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
platform: "windows"
installers:
- executable: "powershell"
installScript: |
Expand All @@ -6,5 +7,7 @@ installers:
- executable: "powershell"
installScript: |
echo "Setting up environment"
apt-get update
apt-get install -y curl

- executable: "cmd.exe"
installScript: |
echo hello world
9 changes: 9 additions & 0 deletions testdata/config_unix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
platform: "unix"
installers:
- executable: "usr/bin/bash"
installScript: |
echo "Hello World"

- executable: "usr/bin/python"
installScript: |
print("hello world")