Skip to content

Commit

Permalink
feature: support nfs.
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldin95 committed Mar 7, 2020
1 parent 108c011 commit 00789c7
Show file tree
Hide file tree
Showing 18 changed files with 408 additions and 96 deletions.
49 changes: 40 additions & 9 deletions http/api/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"github.com/danieldin95/lightstar/http/schema"
"github.com/danieldin95/lightstar/libstar"
"github.com/danieldin95/lightstar/storage"
"github.com/danieldin95/lightstar/storage/libvirts"
"github.com/gorilla/mux"
Expand All @@ -12,6 +13,38 @@ import (
type DataStore struct {
}

func DataStore2XML(conf schema.DataStore) libvirts.Pool {
name := storage.PATH.GetStoreID(conf.Name)
path := storage.PATH.Unix(conf.Name)

polXml := libvirts.PoolXML{
Type: conf.Type,
Name: name,
Target: libvirts.TargetXML{
Path: path,
},
}
if conf.Type == "netfs" && conf.NFS != nil {
polXml.Source = libvirts.SourceXML{
Host: libvirts.HostXML{
Name: conf.NFS.Host,
},
Dir: libvirts.DirXML{
Path: conf.NFS.Path,
},
Format: libvirts.FormatXML{
Type: "nfs",
},
}
}
return libvirts.Pool{
Type: conf.Type,
Name: name,
Path: path,
XML: polXml.Encode(),
}
}

func (store DataStore) Router(router *mux.Router) {
router.HandleFunc("/api/datastore", store.GET).Methods("GET")
router.HandleFunc("/api/datastore", store.POST).Methods("POST")
Expand Down Expand Up @@ -52,19 +85,17 @@ func (store DataStore) GET(w http.ResponseWriter, r *http.Request) {
}

func (store DataStore) POST(w http.ResponseWriter, r *http.Request) {
data := &schema.DataStore{}
if err := GetData(r, data); err != nil {
data := schema.DataStore{}
if err := GetData(r, &data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if storage.PATH.IsDataStore(data.Name) {
name := storage.PATH.GetStoreID(data.Name)
path := storage.PATH.Unix(data.Name)
if _, err := libvirts.CreatePool(name, path); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
pol := DataStore2XML(data)
libstar.Debug("DataStore.POST %s", pol.XML)
if err := pol.Create(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ResponseMsg(w, 0, "success")
}
Expand Down
23 changes: 13 additions & 10 deletions http/api/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ func Network2XML(conf schema.Network) libvirtn.NetworkXML {
Bridge: libvirtn.BridgeXML{
Name: conf.Name,
},
Forward: libvirtn.ForwardXML{
}
if conf.Mode != "" {
xmlObj.Forward = &libvirtn.ForwardXML{
Mode: conf.Mode,
},
}
}
if conf.Mode == "nat" {
xmlObj.Bridge.Stp = "on"
xmlObj.Bridge.Delay = "0"
}

if conf.Mode == "route" || conf.Mode == "nat" {
if conf.Address != "" {
xmlObj.IPv4 = &libvirtn.IPv4XML{
Address: conf.Address,
}
Expand All @@ -49,15 +51,16 @@ func Network2XML(conf schema.Network) libvirtn.NetworkXML {
if conf.Netmask != "" {
xmlObj.IPv4.Netmask = conf.Netmask
}
if conf.DHCP != "no" {
xmlObj.IPv4.DHCP = &libvirtn.DHCPXML{
Range: libvirtn.DHCPRangeXML{
Start: conf.RangeStart,
End: conf.RangeEnd,
},
}
}
if conf.DHCP != "no" {
xmlObj.IPv4.DHCP = &libvirtn.DHCPXML{
Range: libvirtn.DHCPRangeXML{
Start: conf.RangeStart,
End: conf.RangeEnd,
},
}
}

return xmlObj
}

Expand Down
11 changes: 11 additions & 0 deletions http/schema/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import (
"unicode"
)

type NFS struct {
Host string `json:"host"`
Path string `json:"path"`
Format string `json:"format"`
}

type DataStore struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Type string `json:"type"`
Format string `json:"format"`
State string `json:"state"`
Capacity uint64 `json:"capacity"` // bytes
Allocation uint64 `json:"allocation"` // bytes
Available uint64 `json:"available"` // Bytes
Source string `json:"source"`
NFS *NFS `json:"nfs"`
}

func IsDigit(s string) bool {
Expand Down Expand Up @@ -43,6 +51,9 @@ func NewDataStore(pol libvirts.Pool) DataStore {
if xmlObj.Source.Format.Type == "nfs" {
obj.Source = "nfs://" + xmlObj.Source.Host.Name + xmlObj.Source.Dir.Path
}
if xmlObj.Source.Format.Type == "auto" {
obj.Source = "auto://" + xmlObj.Source.Host.Name + xmlObj.Source.Dir.Path
}
default:
obj.Source = obj.Type + "://" + obj.Name
}
Expand Down
4 changes: 3 additions & 1 deletion http/schema/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func NewNetwork(net libvirtn.Network) Network {
}

xml := libvirtn.NewNetworkXMLFromNet(&net)
obj.Mode = xml.Forward.Mode
if xml.Forward != nil {
obj.Mode = xml.Forward.Mode
}
if xml.IPv4 != nil {
obj.Address = xml.IPv4.Address
obj.Netmask = xml.IPv4.Netmask
Expand Down
3 changes: 3 additions & 0 deletions http/static/js/api/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export class DataStoreApi extends Api {
create(data) {
let your = this;

if (data.format == 'nfs') {
data.nfs = {host: data.host, path: data.path, format: 'nfs'};
}
$.post(your.url(), JSON.stringify(data), function (data, status) {
$(your.tasks).append(AlertSuccess(`create datastore ${data.message}`));
}).fail(function (e) {
Expand Down
2 changes: 1 addition & 1 deletion http/static/js/api/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class NetworkApi extends Api {
create(data) {
let your = this;

if (data.range != "") {
if (data.range && data.range != "") {
let range = data.range.split(',', 2);
if (range.length == 2) {
data.rangeStart = range[0];
Expand Down
9 changes: 5 additions & 4 deletions http/static/js/widget/datastore/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {FormModal} from "../form/modal.js";
import {Option} from "../option.js";
import {Utils} from "../../com/utils.js";

export class DataStoreCreate extends FormModal {
export class DirCreate extends FormModal {
//
constructor (props) {
super(props);
Expand All @@ -16,7 +16,7 @@ export class DataStoreCreate extends FormModal {
let name = {
fresh: function() {
this.selector.find('option').remove();
for (let i = 1; i <= 16; i++) {
for (let i = 1; i <= 9; i++) {
let alias = "datastore@"+Utils.iton(i, 2);
this.selector.append(new Option(alias, alias));
}
Expand All @@ -32,15 +32,16 @@ export class DataStoreCreate extends FormModal {
<div class="modal-dialog modal-dialog-centered model-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="">Create DataStore</h5>
<h5 class="modal-title" id="">New DataStore</h5>
</div>
<form name="datastore-new">
<input type="text" class="d-none" name="type" value="dir"/>
<div id="" class="modal-body">
<div class="form-group row">
<label for="name" class="col-sm-4 col-form-label-sm ">Select datastore</label>
<div class="col-sm-6">
<div class="input-group">
<select class="" name="name">
<select class="select-lg" name="name">
<option value="datastore@01" selected>datastore@01</option>
</select>
</div>
Expand Down
61 changes: 61 additions & 0 deletions http/static/js/widget/datastore/iscsi/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {FormModal} from "../../form/modal.js";
import {Option} from "../../option.js";
import {Utils} from "../../../com/utils.js";

export class iSCSICreate extends FormModal {
//
constructor (props) {
super(props);

this.render();
this.loading();
}

render() {
super.render();
let name = {
fresh: function() {
this.selector.find('option').remove();
for (let i = 1; i <= 16; i++) {
let alias = "datastore@"+Utils.iton(i, 2);
this.selector.append(new Option(alias, alias));
}
},
selector: this.view.find("select[name='name']"),
};

name.fresh();
}

template() {
return (`
<div class="modal-dialog modal-dialog-centered model-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="">New DataStore by iSCSI</h5>
</div>
<form name="datastore-new">
<div id="" class="modal-body">
<div class="form-group row">
<label for="name" class="col-sm-4 col-form-label-sm ">Select datastore</label>
<div class="col-sm-6">
<div class="input-group">
<select class="select-lg" name="name">
<option value="datastore@01" selected>datastore@01</option>
</select>
</div>
</div>
</div>
</div>
<div id="" class="modal-footer">
<div class="mr-0" rol="group">
<button name="finish-btn" class="btn btn-outline-success btn-sm">Finish</button>
<button name="reset-btn" class="btn btn-outline-dark btn-sm" type="reset">Reset</button>
<button name="cancel-btn" class="btn btn-outline-dark btn-sm">Cancel</button>
</div>
</div>
</form>
</div>
</div>`);
}
}
79 changes: 79 additions & 0 deletions http/static/js/widget/datastore/nfs/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {FormModal} from "../../form/modal.js";
import {Option} from "../../option.js";
import {Utils} from "../../../com/utils.js";

export class NFSCreate extends FormModal {
//
constructor (props) {
super(props);

this.render();
this.loading();
}

render() {
super.render();
let name = {
fresh: function() {
this.selector.find('option').remove();
for (let i = 10; i <= 20; i++) {
let alias = "datastore@"+Utils.iton(i, 2);
this.selector.append(new Option(alias, alias));
}
},
selector: this.view.find("select[name='name']"),
};

name.fresh();
}

template() {
return (`
<div class="modal-dialog modal-dialog-centered model-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="">New DataStore by NFS</h5>
</div>
<form name="datastore-new">
<input type="text" class="d-none" name="type" value="netfs"/>
<input type="text" class="d-none" name="format" value="nfs"/>
<div id="" class="modal-body">
<div class="form-group row">
<label for="name" class="col-sm-4 col-form-label-sm ">DataStore</label>
<div class="col-sm-6">
<div class="input-group">
<select class="select-lg" name="name">
<option value="datastore@01" selected>datastore@01</option>
</select>
</div>
</div>
</div>
<div class="form-group row">
<label for="host" class="col-sm-4 col-form-label-sm">Host address</label>
<div class="col-sm-6">
<div class="input-group">
<input type="text" class="form-control form-control-sm" name="host" value=""/>
</div>
</div>
</div>
<div class="form-group row">
<label for="host" class="col-sm-4 col-form-label-sm">Directory path</label>
<div class="col-sm-6">
<div class="input-group">
<input type="text" class="form-control form-control-sm" name="path" value="/"/>
</div>
</div>
</div>
</div>
<div id="" class="modal-footer">
<div class="mr-0" rol="group">
<button name="finish-btn" class="btn btn-outline-success btn-sm">Finish</button>
<button name="reset-btn" class="btn btn-outline-dark btn-sm" type="reset">Reset</button>
<button name="cancel-btn" class="btn btn-outline-dark btn-sm">Cancel</button>
</div>
</div>
</form>
</div>
</div>`);
}
}
Loading

0 comments on commit 00789c7

Please sign in to comment.