Add web server to dev image
This commit is contained in:
133
cmd/runmqdevserver/main.go
Normal file
133
cmd/runmqdevserver/main.go
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
© Copyright IBM Corporation 2018
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"github.com/ibm-messaging/mq-container/internal/command"
|
||||
"github.com/ibm-messaging/mq-container/internal/logger"
|
||||
)
|
||||
|
||||
var log *logger.Logger
|
||||
|
||||
func setPassword(user string, password string) error {
|
||||
cmd := exec.Command("chpasswd")
|
||||
stdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(stdin, "%s:%s", user, password)
|
||||
stdin.Close()
|
||||
_, _, err = command.RunCmd(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Set password for \"%v\" user", user)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getLogFormat() string {
|
||||
return os.Getenv("LOG_FORMAT")
|
||||
}
|
||||
|
||||
func getDebug() bool {
|
||||
debug := os.Getenv("DEBUG")
|
||||
if debug == "true" || debug == "1" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func configureLogger() error {
|
||||
f := getLogFormat()
|
||||
d := getDebug()
|
||||
switch f {
|
||||
case "json":
|
||||
log = logger.NewLogger(os.Stderr, d, true)
|
||||
case "basic":
|
||||
log = logger.NewLogger(os.Stderr, d, false)
|
||||
default:
|
||||
log = logger.NewLogger(os.Stdout, d, false)
|
||||
return fmt.Errorf("invalid value for LOG_FORMAT: %v", f)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func logTerminationf(format string, args ...interface{}) {
|
||||
logTermination(fmt.Sprintf(format, args))
|
||||
}
|
||||
|
||||
// TODO: Duplicated code
|
||||
func logTermination(args ...interface{}) {
|
||||
msg := fmt.Sprint(args)
|
||||
// Write the message to the termination log. This is the default place
|
||||
// that Kubernetes will look for termination information.
|
||||
log.Debugf("Writing termination message: %v", msg)
|
||||
err := ioutil.WriteFile("/dev/termination-log", []byte(msg), 0660)
|
||||
if err != nil {
|
||||
log.Debug(err)
|
||||
}
|
||||
log.Error(msg)
|
||||
}
|
||||
|
||||
func doMain() error {
|
||||
err := configureLogger()
|
||||
if err != nil {
|
||||
logTermination(err)
|
||||
return err
|
||||
}
|
||||
adminPassword, set := os.LookupEnv("MQ_ADMIN_PASSWORD")
|
||||
if set {
|
||||
err = setPassword("admin", adminPassword)
|
||||
if err != nil {
|
||||
logTerminationf("Error setting admin password: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
appPassword, set := os.LookupEnv("MQ_APP_PASSWORD")
|
||||
if set {
|
||||
err = setPassword("app", appPassword)
|
||||
if err != nil {
|
||||
logTerminationf("Error setting app password: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = updateMQSC(set)
|
||||
if err != nil {
|
||||
logTerminationf("Error updating MQSC: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var osExit = os.Exit
|
||||
|
||||
func main() {
|
||||
err := doMain()
|
||||
if err != nil {
|
||||
osExit(1)
|
||||
} else {
|
||||
// Replace this process with runmqserver
|
||||
syscall.Exec("/usr/local/bin/runmqserver", []string{"runmqserver"}, os.Environ())
|
||||
}
|
||||
}
|
||||
57
cmd/runmqdevserver/mqsc.go
Normal file
57
cmd/runmqdevserver/mqsc.go
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
© Copyright IBM Corporation 2018
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"os"
|
||||
)
|
||||
|
||||
func updateMQSC(appPasswordRequired bool) error {
|
||||
var checkClient string
|
||||
if appPasswordRequired {
|
||||
checkClient = "REQUIRED"
|
||||
} else {
|
||||
checkClient = "ASQMGR"
|
||||
}
|
||||
const mqsc string = "/etc/mqm/dev.mqsc"
|
||||
if os.Getenv("MQ_DEV") == "true" {
|
||||
const mqscTemplate string = mqsc + ".tpl"
|
||||
// Re-configure channel if app password not set
|
||||
t, err := template.ParseFiles(mqscTemplate)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
f, err := os.OpenFile(mqsc, os.O_CREATE|os.O_WRONLY, 0660)
|
||||
defer f.Close()
|
||||
err = t.Execute(f, map[string]string{"ChckClnt": checkClient})
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
// TODO: Lookup value for MQM user here?
|
||||
err = os.Chown(mqsc, 999, 999)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
// os.Remove(mqscTemplate)
|
||||
} else {
|
||||
os.Remove(mqsc)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user