diff --git a/main.go b/main.go index ce5b278..1f82ffe 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "io/ioutil" + "os" "os/exec" "path/filepath" @@ -11,7 +12,23 @@ import ( "github.com/ghodss/yaml" ) -var configFile string +var ( + configFile string + postInst string +) + +func check(err error, what string) { + if err != nil { + fmt.Println("debian-builder | " + what + ": " + err.Error()) + return + } +} + +func getDirectory() string { + dir, err := filepath.Abs(filepath.Dir(os.Args[0])) + check(err, "Error getting current directory") + return dir +} func loadConfigToModel(file string) (models.Config, string, error) { config := models.Config{} @@ -22,6 +39,19 @@ func loadConfigToModel(file string) (models.Config, string, error) { return config, location, err } +func buildPostInst() error { + //Setup default postinst + postInst = "#!/bin/sh \nsudo docker-service provision" + + //Get current directory + filepath := getDirectory() + + postinstByte := []byte(postInst) + postinstLocation := filepath + "/.postinst" + err := ioutil.WriteFile(postinstLocation, postinstByte, 0777) + return err +} + func buildDebianPackage(control models.Control, fileName string) *exec.Cmd { args := []string{"-s", "dir", "-t", "deb"} @@ -34,32 +64,46 @@ func buildDebianPackage(control models.Control, fileName string) *exec.Cmd { args = append(args, "-v", version) //Setup After Install - afterInstall := ".postinst" + afterInstall := getDirectory() + "/.postinst" args = append(args, "--after-install", afterInstall) + //Disable Warning for no config + noConfigWarningDisable := "--deb-no-default-config-files" + args = append(args, noConfigWarningDisable) + //Setup Config config := fileName + "=/etc/docker-service/services.d/" + control.Package + "_" + control.Version + ".yaml" args = append(args, config) //Exec fpm + fmt.Printf("debian-builder | %s", args) cmd := exec.Command("fpm", args...) return cmd } - -func main() { +func setupEnvironment() (models.Control, string) { + //Read flags flag.StringVar(&configFile, "config", "test.yaml", "a string var") flag.Parse() + //Load config config, filepath, err := loadConfigToModel(configFile) - if err != nil { - fmt.Println("Error loading config: " + err.Error()) - return - } - output, err := buildDebianPackage(config.Control, filepath).Output() - if err != nil { - fmt.Println("Error Building Package: " + err.Error()) - return - } - fmt.Println("Building Package") - fmt.Printf("%s", output) + check(err, "Error loading config") + + //Write file + postinstErr := buildPostInst() + check(postinstErr, "Error installing postinst File") + + return config.Control, filepath +} + +func main() { + //Setup Environment + control, filepath := setupEnvironment() + + //Build Debian Package + output, err := buildDebianPackage(control, filepath).Output() + check(err, "Error Building Package") + + fmt.Println("debian-builder | Building Package") + fmt.Printf("debian-builder | %s\n", output) } diff --git a/main_test.go b/main_test.go index 496ca6b..ff07689 100644 --- a/main_test.go +++ b/main_test.go @@ -22,3 +22,13 @@ func TestBuildDebianPackage(t *testing.T) { t.Fail() } } + +func TestRunEverything(t *testing.T) { + config, filepath := setupEnvironment() + if config.Package == "" { + t.Fail() + } + if filepath == "" { + t.Fail() + } +} diff --git a/test.yaml.postinst b/test.yaml.postinst new file mode 100755 index 0000000..7874a30 --- /dev/null +++ b/test.yaml.postinst @@ -0,0 +1,2 @@ +#!/bin/sh + sudo docker-service provision \ No newline at end of file