Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change docker-startup.sh config file handling, config file path detection and offer save path in setup #457

Merged
merged 3 commits into from
Mar 16, 2018
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
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
machine:
environment:
CPATH: "${HOME}/.go_workspace/src/github.com/${CIRCLE_PROJECT_USERNAME}"
CPATH: "${HOME}/.go_workspace/src/github.com/ansible-semaphore"
node:
version: 8.2.0

Expand Down
20 changes: 16 additions & 4 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,26 @@ func doSetup() int {
setup = util.NewConfig()
}

fmt.Printf(" Running: mkdir -p %v..\n", setup.TmpPath)
os.MkdirAll(setup.TmpPath, 0755)
confDir, err := os.Getwd()
if err != nil {
confDir = "/etc/semaphore"
}
fmt.Print(" > Config output directory (default "+confDir+"): ")

configPath := path.Join(setup.TmpPath, "/semaphore_config.json")
fmt.Printf(" Configuration written to %v..\n", configPath)
var answer string
fmt.Scanln(&answer)
if len(answer) > 0 {
confDir = answer
}

fmt.Printf(" Running: mkdir -p %v..\n", confDir)
os.MkdirAll(confDir, 0755)

configPath := path.Join(confDir, "/config.json")
if err := ioutil.WriteFile(configPath, b, 0644); err != nil {
panic(err)
}
fmt.Printf(" Configuration written to %v..\n", configPath)

fmt.Println(" Pinging db..")
util.Config = setup
Expand Down
34 changes: 22 additions & 12 deletions scripts/docker-startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

echoerr() { printf "%s\n" "$*" >&2; }

SEMAPHORE_PLAYBOOK_PATH="${SEMAPHORE_PLAYBOOK_PATH:-/semaphore}"
SEMAPHORE_CONFIG_PATH="${SEMAPHORE_CONFIG_PATH:-/etc/semaphore}"

SEMAPHORE_TMP_PATH="${SEMAPHORE_TMP_PATH:-/tmp/semaphore_data}"
# Semaphore database env config
SEMAPHORE_DB_HOST="${SEMAPHORE_DB_HOST:-127.0.0.1}"
SEMAPHORE_DB_PORT="${SEMAPHORE_DB_PORT:-3306}"
Expand Down Expand Up @@ -30,9 +32,14 @@ SEMAPHORE_LDAP_MAPPING_USERNAME="${SEMAPHORE_LDAP_MAPPING_USERNAME:-uid}"
SEMAPHORE_LDAP_MAPPING_FULLNAME="${SEMAPHORE_LDAP_MAPPING_FULLNAME:-cn}"
SEMAPHORE_LDAP_MAPPING_EMAIL="${SEMAPHORE_LDAP_MAPPING_EMAIL:-mail}"

# create semaphore playbook directory
mkdir -p "${SEMAPHORE_PLAYBOOK_PATH}" || {
echo "Can't create Semaphore playbook path '$SEMAPHORE_PLAYBOOK_PATH'."
# create semaphore temporary directory if non existent
[ -d "${SEMAPHORE_TMP_PATH}" ] || mkdir -p "${SEMAPHORE_TMP_PATH}" || {
echo "Can't create Semaphore tmp path ${SEMAPHORE_TMP_PATH}."
exit 1
}
# create semaphore config directory if non existent
[ -d "${SEMAPHORE_CONFIG_PATH}" ] || mkdir -p "${SEMAPHORE_CONFIG_PATH}" || {
echo "Can't create Semaphore Config path ${SEMAPHORE_CONFIG_PATH}."
exit 1
}

Expand All @@ -49,22 +56,23 @@ while ! mysqladmin ping -h"$SEMAPHORE_DB_HOST" -P "$SEMAPHORE_DB_PORT" -u "$SEMA
sleep 1
done

if [ ! -f "${SEMAPHORE_PLAYBOOK_PATH}/semaphore_config.json" ]; then
echoerr "Generating ${SEMAPHORE_PLAYBOOK_PATH}/config.stdin ..."
cat << EOF > "${SEMAPHORE_PLAYBOOK_PATH}/config.stdin"
# Create a config if it does not exist in the current config path
if [ ! -f "${SEMAPHORE_CONFIG_PATH}/config.json" ]; then
echoerr "Generating ${SEMAPHORE_TMP_PATH}/config.stdin ..."
cat << EOF > "${SEMAPHORE_TMP_PATH}/config.stdin"
${SEMAPHORE_DB_HOST}:${SEMAPHORE_DB_PORT}
${SEMAPHORE_DB_USER}
${SEMAPHORE_DB_PASS}
${SEMAPHORE_DB}
${SEMAPHORE_PLAYBOOK_PATH}
${SEMAPHORE_TMP_PATH}
${SEMAPHORE_WEB_ROOT}
no
no
${SEMAPHORE_LDAP_ACTIVATED}
EOF

if [ "${SEMAPHORE_LDAP_ACTIVATED}" = "yes" ]; then
cat << EOF >> "${SEMAPHORE_PLAYBOOK_PATH}/config.stdin"
cat << EOF >> "${SEMAPHORE_TMP_PATH}/config.stdin"
${SEMAPHORE_LDAP_HOST}:${SEMAPHORE_LDAP_PORT}
${SEMAPHORE_LDAP_NEEDTLS}
${SEMAPHORE_LDAP_DN_BIND}
Expand All @@ -78,16 +86,18 @@ ${SEMAPHORE_LDAP_MAPPING_EMAIL}
EOF
fi;

cat << EOF >> "${SEMAPHORE_PLAYBOOK_PATH}/config.stdin"
cat << EOF >> "${SEMAPHORE_TMP_PATH}/config.stdin"
yes
${SEMAPHORE_CONFIG_PATH}
${SEMAPHORE_ADMIN}
${SEMAPHORE_ADMIN_EMAIL}
${SEMAPHORE_ADMIN_NAME}
${SEMAPHORE_ADMIN_PASSWORD}
EOF
/usr/bin/semaphore -setup < "${SEMAPHORE_PLAYBOOK_PATH}/config.stdin"

ln -s "${SEMAPHORE_PLAYBOOK_PATH}/semaphore_config.json" /etc/semaphore/semaphore_config.json
cat "${SEMAPHORE_TMP_PATH}/config.stdin"
$1 -setup - < "${SEMAPHORE_TMP_PATH}/config.stdin"
echoerr "Run Semaphore with semaphore -config ${SEMAPHORE_CONFIG_PATH}/config.json"
fi

# run our command
Expand Down
62 changes: 44 additions & 18 deletions util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type configType struct {
// Format `:port_num` eg, :3000
Port string `json:"port"`

// semaphore stores projects here
// semaphore stores ephemeral projects here
TmpPath string `json:"tmp_path"`

// cookie hashing & encryption
Expand Down Expand Up @@ -76,6 +76,7 @@ type configType struct {
}

var Config *configType
var confPath *string

func NewConfig() *configType {
return &configType{}
Expand All @@ -85,7 +86,7 @@ func ConfigInit() {
flag.BoolVar(&InteractiveSetup, "setup", false, "perform interactive setup")
flag.BoolVar(&Migration, "migrate", false, "execute migrations")
flag.BoolVar(&Upgrade, "upgrade", false, "upgrade semaphore")
path := flag.String("config", "", "config path")
confPath = flag.String("config", "", "config path")

var unhashedPwd string
flag.StringVar(&unhashedPwd, "hash", "", "generate hash of given password")
Expand Down Expand Up @@ -132,21 +133,7 @@ func ConfigInit() {
os.Exit(0)
}

if path != nil && len(*path) > 0 {
// load
file, err := os.Open(*path)
if err != nil {
panic(err)
}

if err := json.NewDecoder(file).Decode(&Config); err != nil {
fmt.Println("Could not decode configuration!")
panic(err)
}
} else {
fmt.Println("Cannot Find configuration! Use -c parameter to point to a JSON file generated by -setup.\n\n Hint: have you run `-setup` ?")
os.Exit(1)
}
loadConfig()

if len(os.Getenv("PORT")) > 0 {
Config.Port = ":" + os.Getenv("PORT")
Expand Down Expand Up @@ -178,6 +165,45 @@ func ConfigInit() {
}
}

func loadConfig(){

//If the confPath option has been set try to load and decode it
if confPath != nil && len(*confPath) > 0 {
file, err := os.Open(*confPath)
if err != nil {
panic(err)
}
decodeConfig(file)
} else {

// if no confPath look in the cwd
cwd, err := os.Getwd()
cwd = cwd+"/config.json"
confPath = &cwd
if err == nil {
file, err := os.Open(*confPath)
if err == nil {
decodeConfig(file)
}
}
if err != nil {
fmt.Println("Cannot Find configuration! Use -c parameter to point to a JSON file generated by -setup.\n\n Hint: have you run `-setup` ?")
os.Exit(1)
}

}
fmt.Println("Using config file: "+ *confPath)

}


func decodeConfig(file *os.File){
if err := json.NewDecoder(file).Decode(&Config); err != nil {
fmt.Println("Could not decode configuration!")
panic(err)
}
}

func (conf *configType) GenerateCookieSecrets() {
hash := securecookie.GenerateRandomKey(32)
encryption := securecookie.GenerateRandomKey(32)
Expand Down Expand Up @@ -208,7 +234,7 @@ func (conf *configType) Scan() {
conf.MySQL.DbName = "semaphore"
}

fmt.Print(" > Playbook path: ")
fmt.Print(" > Playbook path (default /tmp/semaphore): ")
fmt.Scanln(&conf.TmpPath)

if len(conf.TmpPath) == 0 {
Expand Down