<commit_before>package mgr

import (
	"bufio"
	"errors"
	"os"
	"regexp"
	"strings"

	log "github.com/Sirupsen/logrus"
	xplatform "github.com/dvonthenen/goxplatform"

	common "github.com/codedellemc/scaleio-framework/scaleio-executor/executor/common"
	types "github.com/codedellemc/scaleio-framework/scaleio-scheduler/types"
)

//constants for verifying that the command lines executed properly
const (
	rexrayBintrayRootURI = "https://dl.bintray.com/emccode/rexray/"
)

const (
	initUnknown = iota
	initSystemD
	initUpdateRcD
	initChkConfig
)

var (
	//ErrSystemInitUnknown Unable to determine the system init type
	ErrSystemInitUnknown = errors.New("Unable to determine the system init type")

	//ErrAddDependencyFailed Failed to add the scini dependency to REX-Ray
	ErrAddDependencyFailed = errors.New("Failed to add the scini dependency to REX-Ray")
)

func getInitSystemType() int {
	log.Debugln("getInitSystemType ENTER")

	if xplatform.GetInstance().Run.ExecExistsInPath("systemctl") {
		log.Debugln("getInitSystemType = initSystemD")
		log.Debugln("getInitSystemType LEAVE")
		return initSystemD
	}

	if xplatform.GetInstance().Run.ExecExistsInPath("update-rc.d") {
		log.Debugln("getInitSystemType = initUpdateRcD")
		log.Debugln("getInitSystemType LEAVE")
		return initUpdateRcD
	}

	if xplatform.GetInstance().Run.ExecExistsInPath("chkconfig") {
		log.Debugln("getInitSystemType = initChkConfig")
		log.Debugln("getInitSystemType LEAVE")
		return initChkConfig
	}

	log.Debugln("getInitSystemType = initUnknown")
	log.Debugln("getInitSystemType LEAVE")
	return initUnknown
}

func getRexrayVersionFromBintray(state *types.ScaleIOFramework) (string, error) {
	url := rexrayBintrayRootURI + state.Rexray.Branch
	version, err := xplatform.GetInstance().Inst.GetVersionFromBintray(url)
	return version, err
}

func getRexrayVersionToInstall(state *types.ScaleIOFramework) (string, error) {
	if state.Rexray.Version == "latest" {
		version, err := getRexrayVersionFromBintray(state)
		return version, err
	}

	return state.Rexray.Version, nil
}

func fixSciniDepInRexrayInitD() error {
	log.Debugln("fixSciniDepInRexrayInitD ENTER")

	writeSciniCmdline := "sed -i 's/\\/usr\\/bin\\/rexray start/if \\[ -e \\/etc\\/init.d\\/scini \\]\\; then \\/etc\\/init.d\\/scini start; fi\\n    \\/usr\\/bin\\/rexray start/' /etc/init.d/rexray"
	output, errScini := xplatform.GetInstance().Run.CommandOutput(writeSciniCmdline)
	if errScini != nil {
		log.Errorln("Failed to add Scini dependency:", errScini)
		log.Debugln("fixSciniDepInRexrayInitD LEAVE")
		return errScini
	}
	if len(output) > 0 {
		log.Errorln("Output Error:", output)
		log.Debugln("fixSciniDepInRexrayInitD LEAVE")
		return ErrAddDependencyFailed
	}

	log.Debugln("Scini has been configured as a dependency on REX-Ray InitD")
	log.Debugln("fixSciniDepInRexrayInitD LEAVE")

	return nil
}

func doesSciniExistInRexrayInitD() (bool, error) {
	log.Debugln("doesSciniExistInRexrayInitD LEAVE")

	file, err := os.Open("/etc/init.d/rexray")
	if err != nil {
		log.Debugln("Failed on file Open:", err)<commit_msg>Fix the rexray/scini dependency for Ubuntu's initd script<commit_after>package mgr

import (
	"bufio"
	"errors"
	"os"
	"regexp"
	"strings"

	log "github.com/Sirupsen/logrus"
	xplatform "github.com/dvonthenen/goxplatform"

	common "github.com/codedellemc/scaleio-framework/scaleio-executor/executor/common"
	types "github.com/codedellemc/scaleio-framework/scaleio-scheduler/types"
)

//constants for verifying that the command lines executed properly
const (
	rexrayBintrayRootURI = "https://dl.bintray.com/emccode/rexray/"
)

const (
	initUnknown = iota
	initSystemD
	initUpdateRcD
	initChkConfig
)

var (
	//ErrSystemInitUnknown Unable to determine the system init type
	ErrSystemInitUnknown = errors.New("Unable to determine the system init type")

	//ErrAddDependencyFailed Failed to add the scini dependency to REX-Ray
	ErrAddDependencyFailed = errors.New("Failed to add the scini dependency to REX-Ray")
)

func getInitSystemType() int {
	log.Debugln("getInitSystemType ENTER")

	if xplatform.GetInstance().Run.ExecExistsInPath("systemctl") {
		log.Debugln("getInitSystemType = initSystemD")
		log.Debugln("getInitSystemType LEAVE")
		return initSystemD
	}

	if xplatform.GetInstance().Run.ExecExistsInPath("update-rc.d") {
		log.Debugln("getInitSystemType = initUpdateRcD")
		log.Debugln("getInitSystemType LEAVE")
		return initUpdateRcD
	}

	if xplatform.GetInstance().Run.ExecExistsInPath("chkconfig") {
		log.Debugln("getInitSystemType = initChkConfig")
		log.Debugln("getInitSystemType LEAVE")
		return initChkConfig
	}

	log.Debugln("getInitSystemType = initUnknown")
	log.Debugln("getInitSystemType LEAVE")
	return initUnknown
}

func getRexrayVersionFromBintray(state *types.ScaleIOFramework) (string, error) {
	url := rexrayBintrayRootURI + state.Rexray.Branch
	version, err := xplatform.GetInstance().Inst.GetVersionFromBintray(url)
	return version, err
}

func getRexrayVersionToInstall(state *types.ScaleIOFramework) (string, error) {
	if state.Rexray.Version == "latest" {
		version, err := getRexrayVersionFromBintray(state)
		return version, err
	}

	return state.Rexray.Version, nil
}

func fixSciniDepInRexrayInitD() error {
	log.Debugln("fixSciniDepInRexrayInitD ENTER")

	writeSciniCmdline1 := "sed -i 's/# Required-Start:    $remote_fs $syslog/# Required-Start:    $remote_fs $syslog scini/' /etc/init.d/rexray"
	output1, errScini1 := xplatform.GetInstance().Run.CommandOutput(writeSciniCmdline1)
	if errScini1 != nil {
		log.Errorln("Failed to add Scini dependency:", errScini1)
		log.Debugln("fixSciniDepInRexrayInitD LEAVE")
		return errScini1
	}
	if len(output1) > 0 {
		log.Errorln("Output Error:", output1)
		log.Debugln("fixSciniDepInRexrayInitD LEAVE")
		return ErrAddDependencyFailed
	}

	writeSciniCmdline2 := "sed -i 's/\\/usr\\/bin\\/rexray start/if \\[ -e \\/etc\\/init.d\\/scini \\]\\; then \\/etc\\/init.d\\/scini start; fi\\n    \\/usr\\/bin\\/rexray start/' /etc/init.d/rexray"
	output2, errScini2 := xplatform.GetInstance().Run.CommandOutput(writeSciniCmdline2)
	if errScini2 != nil {
		log.Errorln("Failed to add Scini dependency:", errScini2)
		log.Debugln("fixSciniDepInRexrayInitD LEAVE")
		return errScini2
	}
	if len(output2) > 0 {
		log.Errorln("Output Error:", output2)
		log.Debugln("fixSciniDepInRexrayInitD LEAVE")
		return ErrAddDependencyFailed
	}

	log.Debugln("Scini has been configured as a dependency on REX-Ray InitD")
	log.Debugln("fixSciniDepInRexrayInitD LEAVE")

	return nil
}

func doesSciniExistInRexrayInitD() (bool, error) {
	log.Debugln("doesSciniExistInRexrayInitD LEAVE")

	file, err := os.Open("/etc/init.d/rexray")
	if err != nil {
		log.Debugln("Failed on file Open:", err)
{'fraction_non_alphanumeric': 0.12403216200119119, 'fraction_numerical': 0.0031268612269207863, 'mean_word_length': 5.0134288272157566, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3223949', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2385, 'n_words': 466}