cisco

Home Shitty cloud provider
Log | Files | Refs | Submodules | git clone https://git.ne02ptzero.me/git/cisco

commit af6c12ae57877c38a069702777574b7cd54990d6
parent 7bc7093ffe53c0d832290a5f08570388167003b1
Author: Louis Solofrizzo <louis@ne02ptzero.me>
Date:   Thu,  4 Apr 2019 23:29:23 +0200

Front & API: Add stop, start and reboot

Signed-off-by: Louis Solofrizzo <louis@ne02ptzero.me>

Diffstat:
Mapi/instance.go | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mapi/main.go | 3+++
Mfront/src/components/Instance.vue | 33+++++++++++++++++++++++++++++++--
3 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/api/instance.go b/api/instance.go @@ -249,6 +249,56 @@ func instance_domain(ctx iris.Context) { ctx.StatusCode(iris.StatusAccepted) } +func instance_start(ctx iris.Context) { + id, _ := ctx.Params().GetInt64("id") + instance := Instance{ID: id} + ok, _ := Database.Get(&instance) + + if !ok { + ctx.StatusCode(iris.StatusNotFound) + return + } + + err := startInstance(&instance) + if err != nil { + ctx.StatusCode(iris.StatusInternalServerError) + } +} + +func instance_stop(ctx iris.Context) { + id, _ := ctx.Params().GetInt64("id") + instance := Instance{ID: id} + ok, _ := Database.Get(&instance) + + if !ok { + ctx.StatusCode(iris.StatusNotFound) + return + } + err := stopInstance(&instance) + if err != nil { + ctx.StatusCode(iris.StatusInternalServerError) + } +} + +func instance_reboot(ctx iris.Context) { + id, _ := ctx.Params().GetInt64("id") + instance := Instance{ID: id} + ok, _ := Database.Get(&instance) + + if !ok { + ctx.StatusCode(iris.StatusNotFound) + return + } + err := stopInstance(&instance) + if err != nil { + err = startInstance(&instance) + } + + if err != nil { + ctx.StatusCode(iris.StatusInternalServerError) + } +} + func instance_create(instance *Instance) { log.Infof("Choosing slave for instance %s", instance.Name) err := chooseSlave(instance) diff --git a/api/main.go b/api/main.go @@ -47,6 +47,9 @@ func main() { app.Get("/api/instance/{id:int32}", instance_get) app.Get("/api/instance/{id:int32}/network", instance_network) app.Post("/api/instance/{id:int32}/bind", instance_bind) + app.Put("/api/instance/{id:int32}/start", instance_start) + app.Put("/api/instance/{id:int32}/stop", instance_stop) + app.Put("/api/instance/{id:int32}/reboot", instance_reboot) app.Delete("/api/instance/{id:int32}", instance_delete) app.Put("/api/instance/{id:int32}/resize/{new_size:string}", instance_resize) app.Put("/api/instance/{id:int32}/domain/{domain:string}", instance_domain) diff --git a/front/src/components/Instance.vue b/front/src/components/Instance.vue @@ -11,11 +11,18 @@ <span class='instance-title'>{{ instance.name }}</span> </b-col> <b-col align="right" class="on-check"> - <b-button variant="outline-danger">Reboot</b-button> + <b-button + variant="outline-danger" + v-on:click="reboot" + > + Reboot + </b-button> <toggle-button - :value="true" + @change="stopStart" + v-model="instance.running" height=35 width=60 + :sync="true" :labels="{checked: 'On', unchecked: 'Off'}" style="font-weight: bold; margin-top: 6px" /> @@ -279,6 +286,9 @@ withCredentials: true }).then((response) => { this.instance = response.data + if (this.instance.text_status == "running" || this.instance.text_status == "installing") { + this.instance.running = true + } }) }, getNetwork() { @@ -348,6 +358,25 @@ }).then((response) => { event.target.innerHTML = 'Add domain' }) + }, + stopStart() { + if (!this.instance.running) + var url = '/api/instance/' + this.$route.params.id + '/stop' + else + var url = '/api/instance/' + this.$route.params.id + '/start' + + this.axios({ + url: url, + withCredentials: true, + method: 'put' + }) + }, + reboot() { + this.axios({ + url: '/api/instance/' + this.$route.params.id + '/reboot', + withCredentials: true, + method: 'put' + }) } } }