Commit 5dcb271b authored by Nick Busey's avatar Nick Busey

Adding Go files, updating Dockerfile

parent 4fbcb40e
Pipeline #65894302 passed with stage
in 37 seconds
......@@ -10,3 +10,4 @@ settings/
/site
/.vscode
.DS_Store
pkg/
\ No newline at end of file
......@@ -55,12 +55,17 @@ RUN set -x && \
mkdir -p /etc/ansible /ansible && \
echo "[local]" >> /etc/ansible/hosts && \
echo "localhost" >> /etc/ansible/hosts && \
\
echo "==> Installing necessities..." && \
wget https://releases.hashicorp.com/terraform/0.12.0/terraform_0.12.0_linux_amd64.zip && \
unzip terraform_0.12.0_linux_amd64.zip && \
mv terraform /usr/local/bin && \
wget https://github.com/mikefarah/yq/releases/download/2.4.0/yq_linux_amd64 && \
chmod +x yq_linux_amd64 && \
mv yq_linux_amd64 /usr/local/bin
mv yq_linux_amd64 /usr/local/bin && \
wget https://gitlab.com/NickBusey/HomelabOS/-/archive/master/HomelabOS-master.zip && \
unzip HomelabOS-master.zip && \
mv HomelabOS-master HomelabOS
ENV ANSIBLE_GATHERING smart
ENV ANSIBLE_HOST_KEY_CHECKING false
......
package cmd
import (
"fmt"
"github.com/spf13/viper"
"github.com/spf13/cobra"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Edit HomelabOS configuration",
Long: `Get and set various HomelabOS configuration values`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Run `config get` or `config set`")
},
}
var configGetCmd = &cobra.Command{
Use: "get",
Short: "Read a HomelabOS configuration value",
Long: `Get a HomelabOS configuration value`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Get!")
},
}
var configSetCmd = &cobra.Command{
Use: "set",
Short: "Write a HomelabOS configuration value",
Long: `Set a HomelabOS configuration value`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(viper.ConfigFileUsed())
// fmt.Println(viper.Unmarshal())
viper.WriteConfig()
},
}
func init() {
configCmd.AddCommand(configGetCmd)
configCmd.AddCommand(configSetCmd)
rootCmd.AddCommand(configCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// configCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
configSetCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
package cmd
import (
"bufio"
"fmt"
"os/exec"
"strings"
"github.com/spf13/cobra"
)
// deployCmd represents the deploy command
var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy HomelabOS",
Long: `This command will deploy HomelabOS to any servers that are configured,
including cloud bastion hosts.`,
Run: func(cmd *cobra.Command, args []string) {
cmdName := "docker"
cmdArgs := []string{"run", "homelabos", "ansible-playbook", "-i", "inventory", "/HomelabOS/playbook.homelabos.yml"}
fmt.Println(RunCmd(cmdName, cmdArgs))
},
}
func init() {
rootCmd.AddCommand(deployCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// deployCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// deployCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// RunCmd takes a command and args and runs it, streaming output to stdout
func RunCmd(cmdName string, cmdArgs []string) error {
fmt.Printf("==> Running: %s %s\n", cmdName, strings.Join(cmdArgs, " "))
//return fmt.Errorf("bye")
cmd := exec.Command(cmdName, cmdArgs...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
return err
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Printf("%s\n", scanner.Text())
}
}()
err = cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "homelabosCtl",
Short: "Control for HomelabOS",
Long: `homelabosCtl is a way to interact with your HomelabOS installation.`,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.homelabos/homelabosCtl.yaml)")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".homelabosCtl" (without extension).
viper.AddConfigPath(home + "/.homelabos")
viper.SetConfigName("homelabosCtl")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strings"
"gitlab.com/nickbusey/homelabosCtl/cmd"
)
func main() {
// fileURL := "https://gitlab.com/NickBusey/HomelabOS/raw/master/Dockerfile"
// if err := DownloadFile("/private/tmp/Dockerfile.HomelabOS", fileURL); err != nil {
// panic(err)
// }
// fmt.Println("deploy called")
// fmt.Println(RunCmd("docker", []string{"build", "-f", "/private/tmp/Dockerfile.HomelabOS", ".", "-t", "homelabos"}))
cmd.Execute()
}
// RunCmd takes a command and args and runs it, streaming output to stdout
func RunCmd(cmdName string, cmdArgs []string) error {
fmt.Printf("==> Running: %s %s\n", cmdName, strings.Join(cmdArgs, " "))
//return fmt.Errorf("bye")
cmd := exec.Command(cmdName, cmdArgs...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
return err
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Printf("%s\n", scanner.Text())
}
}()
err = cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}
// DownloadFile does just that.
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment