forked from debarshibasak/go-multipass
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delete, info and launch methods added
- Loading branch information
Showing
5 changed files
with
221 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package multipass | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
type DeleteRequest struct { | ||
Name string | ||
} | ||
|
||
func DeleteAll() error { | ||
cmd := fmt.Sprintf("multipass delete --all -p") | ||
|
||
cmdExec := exec.Command("sh", "-c", cmd) | ||
|
||
out, err := cmdExec.CombinedOutput() | ||
if err != nil { | ||
fmt.Println(string(out)) | ||
return err | ||
} | ||
|
||
fmt.Println(string(out)) | ||
|
||
return nil | ||
} | ||
|
||
func Delete(req *DeleteRequest) error { | ||
|
||
cmd := fmt.Sprintf("multipass delete -p "+req.Name) | ||
|
||
cmdExec := exec.Command("sh", "-c", cmd) | ||
|
||
out, err := cmdExec.CombinedOutput() | ||
if err != nil { | ||
fmt.Println(string(out)) | ||
return err | ||
} | ||
|
||
fmt.Println(string(out)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package multipass | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
type InfoRequest struct { | ||
Name string | ||
} | ||
|
||
func Info(req *InfoRequest) (*Instance, error) { | ||
|
||
cmdFormat := "multipass info "+ req.Name | ||
|
||
cmdExec := exec.Command("sh", "-c", cmdFormat) | ||
|
||
out, err := cmdExec.Output() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return parseInstance(string(out)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package multipass | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type LaunchReq struct { | ||
CPU int | ||
Disk string | ||
Name string | ||
Memory string | ||
CloudInitFile string | ||
} | ||
|
||
func Launch(launchReq *LaunchReq) (*Instance, error) { | ||
|
||
var args = "" | ||
|
||
if launchReq.CPU != 0 { | ||
args = args + fmt.Sprintf("--cpus %v", launchReq.CPU) | ||
} | ||
|
||
if launchReq.Name != "" { | ||
args = args + fmt.Sprintf("--name %v", launchReq.Name) | ||
} | ||
|
||
if launchReq.Disk != "" { | ||
args = args + fmt.Sprintf("--disk %v", launchReq.Disk) | ||
} | ||
|
||
if launchReq.Memory != "" { | ||
args = args + fmt.Sprintf("--mem %v", launchReq.Memory) | ||
} | ||
|
||
if launchReq.CloudInitFile != "" { | ||
args = args + fmt.Sprintf("--cloud-init %v", launchReq.CloudInitFile) | ||
} | ||
|
||
cmd := fmt.Sprintf("multipass launch "+args) | ||
|
||
fmt.Println(cmd) | ||
|
||
cmdExec := exec.Command("sh", "-c", cmd) | ||
|
||
out, err := cmdExec.CombinedOutput() | ||
if err != nil { | ||
fmt.Println(string(out)) | ||
return nil, err | ||
} | ||
|
||
var b []byte | ||
b = append(b, out...) | ||
|
||
out2 := string(b) | ||
|
||
o := strings.Split(strings.TrimSpace(out2), "\n")[0] | ||
|
||
name := strings.TrimSpace(strings.Split(o, "Launched: ")[1]) | ||
|
||
instance, err := Info(&InfoRequest{Name:name}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return instance, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,75 @@ | ||
package multipass | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
import "strings" | ||
|
||
const ( | ||
Name = "Name:" | ||
State = "State:" | ||
IPv4 = "IPv4:" | ||
Release = "Release:" | ||
ImageHash = "Image hash:" | ||
Load = "Load:" | ||
DiskUsage = "Disk usage:" | ||
MemoryUsage = "Memory usage:" | ||
) | ||
|
||
type LaunchReq struct { | ||
CPU int | ||
Disk string | ||
Name string | ||
Memory string | ||
CloudInitFile string | ||
type Instance struct { | ||
Name string | ||
IP string | ||
State string | ||
Image string | ||
ImageHash string | ||
DiskUsage string | ||
TotalDisk string | ||
MemoryUsage string | ||
MemoryTotal string | ||
Load string | ||
} | ||
|
||
func Launch(launchReq *LaunchReq) error { | ||
func parseInstance(out string) *Instance { | ||
|
||
var args = "" | ||
var instance Instance | ||
|
||
if launchReq.CPU != 0 { | ||
args = args + fmt.Sprintf("--cpus %v", launchReq.CPU) | ||
} | ||
for _, line := range strings.Split(out, "\n") { | ||
|
||
if launchReq.Name != "" { | ||
args = args + fmt.Sprintf("--name %v", launchReq.Name) | ||
} | ||
if strings.Contains(line, Name) { | ||
instance.Name = strings.TrimSpace(strings.ReplaceAll(line, Name, "")) | ||
} | ||
|
||
if launchReq.Disk != "" { | ||
args = args + fmt.Sprintf("--disk %v", launchReq.Disk) | ||
} | ||
if strings.Contains(line, State) { | ||
instance.State = strings.TrimSpace(strings.ReplaceAll(line, State, "")) | ||
} | ||
|
||
if launchReq.Memory != "" { | ||
args = args + fmt.Sprintf("--mem %v", launchReq.Memory) | ||
} | ||
if strings.Contains(line, IPv4) { | ||
instance.IP = strings.TrimSpace(strings.ReplaceAll(line, IPv4, "")) | ||
} | ||
|
||
if launchReq.CloudInitFile != "" { | ||
args = args + fmt.Sprintf("--cloud-init %v", launchReq.CloudInitFile) | ||
} | ||
if strings.Contains(line, Release) { | ||
instance.Image = strings.TrimSpace(strings.ReplaceAll(line, Release, "")) | ||
} | ||
|
||
cmd := fmt.Sprintf("multipass launch "+args) | ||
if strings.Contains(line, ImageHash) { | ||
instance.ImageHash = strings.TrimSpace(strings.ReplaceAll(line, ImageHash, "")) | ||
} | ||
|
||
cmdExec := exec.Command("sh", "-c", cmd) | ||
if strings.Contains(line, Load) { | ||
instance.Load = strings.TrimSpace(strings.ReplaceAll(line, Load, "")) | ||
} | ||
|
||
out, err := cmdExec.CombinedOutput() | ||
if err != nil { | ||
fmt.Println(string(out)) | ||
return err | ||
} | ||
if strings.Contains(line, DiskUsage) { | ||
diskUsage := strings.TrimSpace(strings.ReplaceAll(line, DiskUsage, "")) | ||
diskUsageOut := strings.Split(diskUsage, "out of") | ||
instance.DiskUsage = strings.TrimSpace(diskUsageOut[0]) | ||
instance.TotalDisk = strings.TrimSpace(diskUsageOut[1]) | ||
} | ||
|
||
fmt.Println(string(out)) | ||
if strings.Contains(line, MemoryUsage) { | ||
memoryUsage := strings.TrimSpace(strings.ReplaceAll(line, MemoryUsage, "")) | ||
memoryUsageOut := strings.Split(memoryUsage, "out of") | ||
instance.MemoryUsage = strings.TrimSpace(memoryUsageOut[0]) | ||
instance.MemoryTotal = strings.TrimSpace(memoryUsageOut[1]) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
return &instance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,38 @@ | ||
package multipass_test | ||
package multipass | ||
|
||
import ( | ||
"github.com/debarshibasak/go-multipass/multipass" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
if err := multipass.Launch(&multipass.LaunchReq{ | ||
func TestCreateInfoAndDelete(t *testing.T) { | ||
instance, err := Launch(&LaunchReq{ | ||
CPU: 2, | ||
}); err != nil { | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Log("created instance "+instance.IP) | ||
t.Log("now deleting "+instance.Name) | ||
|
||
if err := Delete(&DeleteRequest{Name:instance.Name}); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestParsing(t *testing.T) { | ||
|
||
instance := parseInstance(`Name: perennial-trogon | ||
State: Running | ||
IPv4: 192.168.64.55 | ||
Release: Ubuntu 18.04.4 LTS | ||
Image hash: 3c3a67a14257 (Ubuntu 18.04 LTS) | ||
Load: 0.06 0.07 0.02 | ||
Disk usage: 988.2M out of 4.7G | ||
Memory usage: 83.6M out of 985.6M`) | ||
|
||
|
||
fmt.Printf(instance.IP) | ||
|
||
} |