mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-22 06:57:49 +00:00
Try to do systemd calls in gosuper
This commit is contained in:
parent
82f87527c8
commit
6f42fc2036
2
Makefile
2
Makefile
@ -66,7 +66,7 @@ gosuper: go-builder
|
||||
|
||||
test-gosuper: go-builder
|
||||
-docker rm --volumes -f resin_test_gosuper_$(JOB_NAME) || true
|
||||
docker run --name resin_test_gosuper_$(JOB_NAME) resin/go-supervisor-builder:$(SUPERVISOR_VERSION) bash -c "cd src/resin-supervisor/gosuper && ./test_formatting.sh && go test -v ./gosuper"
|
||||
docker run --name resin_test_gosuper_$(JOB_NAME) -v /var/run/dbus:/mnt/root/run/dbus resin/go-supervisor-builder:$(SUPERVISOR_VERSION) bash -c "cd src/resin-supervisor/gosuper && ./test_formatting.sh && go test -v ./gosuper"
|
||||
docker rm --volumes -f resin_test_gosuper_$(JOB_NAME)
|
||||
|
||||
format-gosuper: go-builder
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"resin-supervisor/gosuper/systemd"
|
||||
)
|
||||
|
||||
type ApiResponse struct {
|
||||
@ -51,12 +53,16 @@ func parsePurgeBody(request *http.Request) (appId string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func responseSender(writer http.ResponseWriter) func(string, string, int) {
|
||||
return func(statusMsg, errorMsg string, statusCode int) {
|
||||
jsonResponse(writer, ApiResponse{statusMsg, errorMsg}, statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func PurgeHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
log.Println("Purging /data")
|
||||
|
||||
sendResponse := func(statusMsg, errorMsg string, statusCode int) {
|
||||
jsonResponse(writer, ApiResponse{statusMsg, errorMsg}, statusCode)
|
||||
}
|
||||
sendResponse := responseSender(writer)
|
||||
sendError := func(err error) {
|
||||
sendResponse("Error", err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
@ -84,3 +90,19 @@ func PurgeHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
sendResponse("OK", "", http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func RebootHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
log.Println("Rebooting")
|
||||
|
||||
sendResponse := responseSender(writer)
|
||||
sendResponse("OK", "", http.StatusAccepted)
|
||||
systemd.Reboot()
|
||||
}
|
||||
|
||||
func ShutdownHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
log.Println("Shutting down")
|
||||
|
||||
sendResponse := responseSender(writer)
|
||||
sendResponse("OK", "", http.StatusAccepted)
|
||||
systemd.Shutdown()
|
||||
}
|
@ -19,6 +19,8 @@ func setupApi(router *mux.Router) {
|
||||
|
||||
apiv1 := router.PathPrefix("/v1").Subrouter()
|
||||
apiv1.HandleFunc("/purge", PurgeHandler).Methods("POST")
|
||||
apiv1.HandleFunc("/reboot", RebootHandler).Methods("POST")
|
||||
apiv1.HandleFunc("/shutdown", ShutdownHandler).Methods("POST")
|
||||
}
|
||||
|
||||
func startApi(listenAddress string, router *mux.Router) {
|
||||
|
34
gosuper/systemd/systemd.go
Normal file
34
gosuper/systemd/systemd.go
Normal file
@ -0,0 +1,34 @@
|
||||
package systemd
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os/exec"
|
||||
|
||||
"resin-supervisor/gosuper/Godeps/_workspace/src/github.com/godbus/dbus"
|
||||
)
|
||||
|
||||
var Systemd dbus.BusObject
|
||||
|
||||
func init() {
|
||||
if conn, err := dbus.Dial("unix:path=/mnt/root/run/dbus/system_bus_socket"); err != nil {
|
||||
log.Fatal("Failed to connect to host system bus")
|
||||
} else {
|
||||
Systemd = conn.Object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
|
||||
}
|
||||
}
|
||||
|
||||
func Reboot() {
|
||||
if err := exec.Command("sync").Run(); err != nil {
|
||||
log.Println(err)
|
||||
} else if call := Systemd.Call("org.freedesktop.systemd1.Reboot", 0); call.Err != nil {
|
||||
log.Println(call.Err)
|
||||
}
|
||||
}
|
||||
|
||||
func Shutdown() {
|
||||
if err := exec.Command("sync").Run(); err != nil {
|
||||
log.Println(err)
|
||||
} else if call := Systemd.Call("org.freedesktop.systemd1.PowerOff", 0); call.Err != nil {
|
||||
log.Println(call.Err)
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@
|
||||
"bluebird": "^2.9.24",
|
||||
"body-parser": "^1.12.0",
|
||||
"coffee-script": "~1.9.1",
|
||||
"dbus-native": "^0.2.0",
|
||||
"dockerode": "~2.2.1",
|
||||
"event-stream": "^3.0.20",
|
||||
"express": "^4.0.0",
|
||||
|
@ -8,7 +8,6 @@ express = require 'express'
|
||||
bodyParser = require 'body-parser'
|
||||
request = require 'request'
|
||||
config = require './config'
|
||||
systemd = require './systemd'
|
||||
|
||||
module.exports = (secret) ->
|
||||
api = express()
|
||||
@ -65,13 +64,13 @@ module.exports = (secret) ->
|
||||
|
||||
api.post '/v1/reboot', (req, res) ->
|
||||
utils.mixpanelTrack('Reboot')
|
||||
res.sendStatus(200)
|
||||
systemd.reboot()
|
||||
request.post(config.gosuperAddress + '/v1/reboot')
|
||||
.pipe(res)
|
||||
|
||||
api.post '/v1/shutdown', (req, res) ->
|
||||
utils.mixpanelTrack('Shutdown')
|
||||
res.sendStatus(200)
|
||||
systemd.shutdown()
|
||||
request.post(config.gosuperAddress + '/v1/shutdown')
|
||||
.pipe(res)
|
||||
|
||||
api.post '/v1/purge', (req, res) ->
|
||||
appId = req.body.appId
|
||||
|
@ -1,37 +0,0 @@
|
||||
Promise = require('bluebird')
|
||||
dbus = require('dbus-native')
|
||||
execAsync = Promise.promisify(require('child_process').exec)
|
||||
systemBus = dbus.systemBus()
|
||||
|
||||
module.exports = systemd =
|
||||
iface: null
|
||||
init: ->
|
||||
return new Promise (resolve, reject) ->
|
||||
systemBus.getService('org.freedesktop.systemd1')
|
||||
.getInterface '/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager', (err, iface) ->
|
||||
return reject(err) if err?
|
||||
systemd.iface = iface
|
||||
resolve()
|
||||
|
||||
proxyMethod: (method)->
|
||||
args = Array.prototype.slice.call(arguments, 1)
|
||||
Promise.try ->
|
||||
return systemd.init() if !systemd.iface
|
||||
.then ->
|
||||
new Promise (resolve, reject) ->
|
||||
cb = (err) ->
|
||||
output = Array.prototype.slice.call(arguments, 1)
|
||||
return reject(err) if err?
|
||||
resolve(output)
|
||||
args.push(cb)
|
||||
systemd.iface[method].apply(systemd.iface, args)
|
||||
|
||||
shutdown: ->
|
||||
execAsync('sync')
|
||||
.then ->
|
||||
systemd.proxyMethod('PowerOff')
|
||||
|
||||
reboot: ->
|
||||
execAsync('sync')
|
||||
.then ->
|
||||
systemd.proxyMethod('Reboot')
|
Loading…
Reference in New Issue
Block a user