cisco

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

commit efcda7cb060d9c3897aa7b9cd59c95411162ade3
parent af6c12ae57877c38a069702777574b7cd54990d6
Author: Louis Solofrizzo <louis@ne02ptzero.me>
Date:   Thu, 11 Apr 2019 19:41:30 +0200

Api & Front: Add the possibility to choose an ansible installation playbook at instance creation

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

Diffstat:
Mapi/CMakeLists.txt | 1+
Mapi/instance.go | 1+
Mapi/main.go | 1+
Aapi/playbook.go | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mapi/slaves.go | 10++++++++++
Mfront/src/components/CreateInstance.vue | 35++++++++++++++++++++++++++++++++++-
6 files changed, 98 insertions(+), 1 deletion(-)

diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt @@ -6,4 +6,5 @@ add_go_component(lf-cloud-api ssh.go slaves.go http.go + playbook.go ) diff --git a/api/instance.go b/api/instance.go @@ -34,6 +34,7 @@ type Instance struct { PhysicalNode string `json:"physical_node" xorm:"varchar(120)"` CreatedAt time.Time `json:"created" xorm:"created"` UpdatedAt time.Time `json:"updated" xorm:"updated"` + Playbooks []string `json:"playbooks"` Domains []string } diff --git a/api/main.go b/api/main.go @@ -53,6 +53,7 @@ func main() { 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) + app.Get("/api/playbooks", playbooks_get) err = ssh_init() if err != nil { diff --git a/api/playbook.go b/api/playbook.go @@ -0,0 +1,51 @@ +package main + +import ( + "github.com/kataras/iris" + log "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" + "io/ioutil" + "os" +) + +type Playbook struct { + Name string `json:"name"` + Desc PlaybookDesc `json:"infos"` +} + +type PlaybookDesc struct { + Description string `json:"description" yaml:"description"` +} + +func playbooks_get(ctx iris.Context) { + var ret []Playbook + + files, err := ioutil.ReadDir("/usr/share/cisco-ansible") + if err != nil { + log.Errorf("Could not open git directory: %v", err) + ctx.StatusCode(iris.StatusInternalServerError) + return + } + + for _, f := range files { + var playbook Playbook + + stat, err := os.Stat("/usr/share/cisco-ansible/" + f.Name()) + if err == nil && stat.IsDir() { + file, err := ioutil.ReadFile("/usr/share/cisco-ansible/" + f.Name() + "/desc.yml") + if err != nil { + log.Errorf("Could not read desc.yml for %s %v", f.Name(), err) + continue + } + err = yaml.Unmarshal(file, &playbook.Desc) + if err != nil { + log.Errorf("Could not unmarshal yaml for %s: %v", f.Name(), err) + continue + } + playbook.Name = f.Name() + ret = append(ret, playbook) + } + } + + ctx.JSON(ret) +} diff --git a/api/slaves.go b/api/slaves.go @@ -298,6 +298,16 @@ func instanceInstallAnsible(instance *Instance) error { body, _ = json.Marshal(exec) _, err = httpRequest("POST", slave, "api/instance/"+instance.Name+"/exec", body) + for _, playbook := range instance.Playbooks { + exec = InstanceExec{ + Cmd: []string{ + "ansible-pull -U git://git.ne02ptzero.me/cisco-ansible " + playbook + "/install.yml", + }, + } + 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}) } else { diff --git a/front/src/components/CreateInstance.vue b/front/src/components/CreateInstance.vue @@ -65,6 +65,25 @@ </b-form-group> </div> + <div> + <b-button v-b-toggle.collapse-1 variant="warning">Advanced</b-button> + <b-collapse id="collapse-1"> + <br /> + <h6>Installation scripts</h6> + <b-form-group> + <b-form-checkbox + v-for="playbook in playbooks" + v-model="selected" + :key="playbook.name" + :value="playbook.name" + name="flavour-2a" + > + <b>{{ playbook.name }}:</b> {{ playbook.infos.description }} + </b-form-checkbox> + </b-form-group> + </b-collapse> + </div> + <br /> <b-button block variant="success" :class="{'disabled': os_choosed === '' || !instance_ok}" @@ -120,6 +139,8 @@ release_choosed: "bionic", instance_name: "", instance_ok: false, + selected: [], + playbooks: [], os: [ { name: "Ubuntu", @@ -196,14 +217,26 @@ ] } }, + created: function() { + this.fetchItems(); + }, methods: { + fetchItems() { + this.axios({ + url: "https://cloud.louifox.house/api/playbooks", + withCredentials: true + }).then((response) => { + this.playbooks = response.data; + }); + }, add_instance(event) { if (this.instance_ok) { var data = { arch: this.arch_choosed, os: this.os_choosed, name: this.instance_name, - release: this.release_choosed + release: this.release_choosed, + playbooks: this.selected, } if (data.arch == "intel") {