Skip to content

Commit

Permalink
fixed issue with postinst script
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcherubini committed Feb 16, 2017
1 parent dd0224b commit a4f26fa
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
74 changes: 59 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"

"github.com/danmademe/debian-builder/models"
"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{}
Expand All @@ -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"}

Expand All @@ -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)
}
10 changes: 10 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
2 changes: 2 additions & 0 deletions test.yaml.postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
sudo docker-service provision

0 comments on commit a4f26fa

Please sign in to comment.