cisco

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

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

Api: Proper status and disable GPG check for download

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

Diffstat:
Mapi/instance.go | 37+++++++++++++++++++++++++++++--------
Mapi/slaves.go | 40+++++++++++++++++++++++++++++++---------
Mfront/index.html | 8++++----
Mfront/src/components/DisplayInstances.vue | 2+-
Mfront/src/components/Instance.vue | 4++--
Mslave/instance.go | 11++++++-----
6 files changed, 73 insertions(+), 29 deletions(-)

diff --git a/api/instance.go b/api/instance.go @@ -12,10 +12,10 @@ type InstanceStatus int const ( InstanceCreated InstanceStatus = iota InstanceAllocated - InstanceStarted InstanceInstalling - InstanceStopped InstanceDeleting + InstanceError + InstanceRunning ) type Instance struct { @@ -43,6 +43,29 @@ func instance_list(ctx iris.Context) { err := Database.Where("owner = ?", cert.Subject.CommonName).Find(&instances) if err == nil { + for i, instance := range instances { + switch instance.Status { + case InstanceCreated: + instance.TextStatus = "created" + case InstanceAllocated: + instance.TextStatus = "allocated" + case InstanceInstalling: + instance.TextStatus = "installing" + case InstanceDeleting: + instance.TextStatus = "deleting" + case InstanceError: + instance.TextStatus = "error" + } + + if instance.Status == InstanceRunning { + err := instanceGet(&instances[i]) + if err != nil { + ctx.StatusCode(iris.StatusInternalServerError) + return + } + } + + } ctx.JSON(&instances) } } @@ -86,16 +109,14 @@ func instance_get(ctx iris.Context) { instance.TextStatus = "allocated" case InstanceInstalling: instance.TextStatus = "installing" - case InstanceStarted: - instance.TextStatus = "running" - case InstanceStopped: - instance.TextStatus = "stopped" case InstanceDeleting: instance.TextStatus = "deleting" + case InstanceError: + instance.TextStatus = "error" } if ok { - if instance.Status == InstanceStarted || instance.Status == InstanceStopped { + if instance.Status == InstanceRunning { err := instanceGet(&instance) if err != nil { ctx.StatusCode(iris.StatusInternalServerError) @@ -246,7 +267,7 @@ func instance_create(instance *Instance) { } /* Wait for DNS & Internet */ - time.Sleep(5 * time.Second) + time.Sleep(20 * time.Second) err = instanceInstallAnsible(instance) if err != nil { diff --git a/api/slaves.go b/api/slaves.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "cisco/contrib/go-lxc" "encoding/json" "errors" log "github.com/sirupsen/logrus" @@ -122,7 +123,7 @@ func createInstance(instance *Instance) error { return err } - Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceStopped}) + Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceCreated}) return nil } @@ -135,7 +136,7 @@ func startInstance(instance *Instance) error { return err } - Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceStarted}) + Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceRunning}) return nil } @@ -148,23 +149,18 @@ func stopInstance(instance *Instance) error { return err } - Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceStopped}) return nil } func rebootInstance(instance *Instance) error { slave := getSlaveByName(instance.PhysicalNode) - instance.Status = InstanceStopped - Database.Update(&instance) - _, err := httpRequest("PUT", slave, "api/instance/"+instance.Name+"/reboot", nil) if err != nil { - log.Errorf("Could not start instance") + log.Errorf("Could not reboot instance") return err } - Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceStarted}) return nil } @@ -202,6 +198,25 @@ func instanceGet(instance *Instance) error { } instance.Gateway = infos.Gateway instance.Capacity = infos.Capacity + + switch lxc.State(infos.Status) { + case lxc.STOPPED: + instance.TextStatus = "stopped" + case lxc.STARTING: + instance.TextStatus = "starting" + case lxc.RUNNING: + instance.TextStatus = "running" + case lxc.STOPPING: + instance.TextStatus = "stopping" + case lxc.ABORTING: + instance.TextStatus = "aborting" + case lxc.FREEZING: + instance.TextStatus = "freezing" + case lxc.FROZEN: + instance.TextStatus = "frozen" + case lxc.THAWED: + instance.TextStatus = "thawed" + } return nil } @@ -271,6 +286,7 @@ func instanceInstallAnsible(instance *Instance) error { body, _ := json.Marshal(exec) _, err := httpRequest("POST", slave, "api/instance/"+instance.Name+"/exec", body) if err != nil { + Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceError}) return err } @@ -281,7 +297,13 @@ func instanceInstallAnsible(instance *Instance) error { } body, _ = json.Marshal(exec) _, err = httpRequest("POST", slave, "api/instance/"+instance.Name+"/exec", body) - Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceStarted}) + + if err != nil { + Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceError}) + } else { + Database.ID(instance.ID).Cols("status").Update(&Instance{Status: InstanceRunning}) + } + return err } diff --git a/front/index.html b/front/index.html @@ -42,19 +42,19 @@ font-size: 0.8em; } - .status-0, .status-1 { + .status-allocated, .status-installing { color: #f3f387; } - .status-2 { + .status-running { color: #38c667; } - .status-3 { + .status-starting { color: #4d953f80; } - .status-4 { + .status-errored { color: #ff000080; } diff --git a/front/src/components/DisplayInstances.vue b/front/src/components/DisplayInstances.vue @@ -11,7 +11,7 @@ > <b-row> <b-col> - <i :class="'fas fa-circle status status-' + instance.status"></i> + <i :class="'fas fa-circle status status-' + instance.text_status"></i> <img :src="'/static/' + instance.os + '.png'" /> {{ instance.name }} </b-col> diff --git a/front/src/components/Instance.vue b/front/src/components/Instance.vue @@ -6,7 +6,7 @@ <div class='instance-header'> <b-row> <b-col sm="9"> - <i :class="'fas fa-circle status status-' + instance.status"></i> + <i :class="'fas fa-circle status status-' + instance.text_status"></i> <img :src="'/static/' + instance.os + '.png'" /> <span class='instance-title'>{{ instance.name }}</span> </b-col> @@ -32,7 +32,7 @@ <th>Physical node:</th> </tr> <tr class='line-bottom'> - <td><i :class="'fas fa-circle status status-' + instance.status"></i> {{ instance.text_status }}</td> + <td><i :class="'fas fa-circle status status-' + instance.text_status"></i> {{ instance.text_status }}</td> <td>{{ instance.arch }}</td> <td>{{ instance.os }}</td> <td><code>{{ instance.physical_node }}</code></td> diff --git a/slave/instance.go b/slave/instance.go @@ -46,11 +46,12 @@ func instance_create(ctx iris.Context) { c.SetVerbosity(lxc.Verbose) options := lxc.TemplateOptions{ - Template: "download", - Distro: instance.Distro, - Release: instance.Release, - Arch: instance.Arch, - Backend: lxc.RBD, + Template: "download", + Distro: instance.Distro, + Release: instance.Release, + Arch: instance.Arch, + Backend: lxc.RBD, + DisableGPGValidation: true, } log.Infof("Creating container %v", options)