# README
go-containers
A pre-made Golang module for easily spinning up and managing lxd containers.
- Author: John Connor Sanders
- License: Apache Version 2.0
- Version Release Date: 04/18/2021
- Current Version: 0.0.3
- Developed for Ubuntu 20.x
License
- Copyright 2021 John Connor Sanders
This source code of this package is released under the Apache Version 2.0 license. Please see the LICENSE for the full content of the license.
Installation
$ go get github.com/JECSand/go-containers
Dependencies
Ensure that LXD is installed and running on your environment
- Use the provided lxd installer script if needed:
$ . ./build_lxd.sh
Module Main Data Structs
###1. GoCluster
type GoCluster struct {
Name string
Type string
ReverseProxy string
LoadBalancer string
Controller string
Containers []*GoContainer
Images []*GoImage
Network *Network
}
-
###GoCluster Methods
I. Scan()
func (gc *GoCluster) Scan() ([]*GoContainers, error)
II. CreateContainer()
func (gc *GoCluster) CreateContainer(auth *Auth, controller bool, name string, cType string, cRelease string, config []byte) error
III. GetContainers()
func (gc *GoCluster) GetContainers() ([]*GoContainer, error)
IV. GetContainer()
func (gc *GoCluster) GetContainer(containerName string) (*GoContainer, error)
V. DeleteContainer()
func (gc *GoCluster) DeleteContainer(containerName string) error
VI. ExportContainer()
func (gc *GoCluster) ExportContainer(containerName string) (*GoImage, error)
VII. ImportContainer()
func (gc *GoCluster) ImportContainer(containerName string, image *GoImage) (*GoContainer, error)
VIII. ScanImages()
func (gc *GoCluster) ScanImages() ([]*GoImage, error)
IX. CreateImage()
func (gc *GoCluster) CreateImage(containerName string, sName string) error
X. DeleteImage()
func (gc *GoCluster) DeleteImage(fingerprint string) error
###2. Network
type Network struct {
PublicIP string
PrivateIP string
HWAddr string
Type string
HostName string
SSL bool
DNS string
Connections []*Connection
}
###3. GoContainer
type GoContainer struct {
Name string
Controller bool
SSHClient *SSHClient
Type string
Release string
Services []string
InitFile []byte
Storage string
Network *Network
Auth *Auth
GoSnapshots []*GoSnapshot
Status string
}
-
###GoContainer Methods
I. Create()
func (c *GoContainer) Create() error
II. Stop()
func (c *GoContainer) Stop() error
II. Boot()
func (c *GoContainer) Boot() error
III. Reboot()
func (c *GoContainer) Reboot() error
IV. Delete()
func (c *GoContainer) Delete() error
V. CreateSnapshot()
func (c *GoContainer) CreateSnapshot() (string, error)
VI. GetSnapshots()
func (c *GoContainer) GetSnapshots() ([]*GoSnapshot, error)
VII. Restore()
func (c *GoContainer) Restore(snapshotName string) error
VIII. Image()
func (c *GoContainer) Image(snapshotName string) (*GoImage, error)
IX. Export()
func (c *GoContainer) Export() (*GoImage, error)
X. Import()
func (c *GoContainer) Import(image *GoImage) error
XI. CMD()
func (c *GoContainer) CMD(cmd string, userName string, reErr bool) ([]byte, error)
XII. OpenSSH()
func (c *GoContainer) OpenSSH() error
###4. GoContainer.Auth
type Auth struct {
User string
Type string
Credential string
SecurityString string
Port string
}
###5. GoContainer.GoSnapshot
type GoSnapshot struct {
Name string
DateTime string
}
###6. GoContainer.SSHClient
type SSHClient struct {
SSHConn *ssh.Client
}
-
###SSHClient Methods
I. Close()
func (ssh *SSHClient) Close() error
###7. GoImage
type GoImage struct {
Name string
Type string
Fingerprint string
TarMeta []string
Contents [][]byte
DateTime string
}
-
###GoImage Methods
I. Import()
func (im *GoImage) Import() error
II. Export()
func (im *GoImage) Export() error
Usage Examples
package main
import (
"fmt"
"github.com/JECSand/go-containers"
"log"
)
func main() {
// #1: Declare a Cluster of LXC GoContainers
goCluster := containers.NewGoCluster("test", "ubuntu", "HA-Proxy", "nginx", "")
// #2: Scan your cluster for existing containers at any time to reload the GoContainer Map
clusterContains, err := goCluster.Scan()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(clusterContains)
// #3: Create a New GoContainer
// -Params: Username, AuthType, Pass/Key, SecretKey, SSH Port
cAuth := containers.NewAuth("envUserName", "password", "envPW", "SecurityString*", "22") // Auth for the GoContainer
// *Note: SecurityString is for SSH Key Auth, which is coming soon
isCluterControllerNode := true
// Create the new GoContainer
// -Params: Auth, isController, ContainerName, ContainerOS, osRelease, CloudInitFile
err = goCluster.CreateContainer(cAuth, isCluterControllerNode, "testContainer", "ubuntu", "xenial", []byte{})
if err != nil {
log.Fatal(err.Error())
}
// #4: Get a GoContainer from GoCluster
goCon, err := goCluster.GetContainer("testContainer")
if err != nil {
log.Fatal(err.Error())
}
goCon.Auth = cAuth
// #5: Open a SSHClient on your GoContainer
err = goCon.OpenSSH()
if err != nil {
log.Fatal(err.Error())
}
// #6: Execute Shell Commands on your GoContainer via SSH
cmd := "echo Hello GoContainer ; sudo apt-get -y update"
sshSession, _ := goCon.SSHClient.SSHConn.NewSession()
defer sshSession.Close()
sshSession.Run(cmd) // Execute the SSH Command
// #7: Close the SSHClient Session
goCon.SSHClient.close()
// #8: Execute a Shell Command on your GoContainer via lxc exec
out, _ := goCon.CMD("pwd", "ubuntu", false)
fmt.Println("Remote Command Output: ", string(out))
// #9: Export GoContainer to GoImage
reImg, err := goCluster.ExportContainer("testContainer")
if err != nil {
log.Fatal("Error Exporting the GoContainer: ", err.Error())
}
// Exported GoContainer's contents stored in tar.gz format
fmt.Println(reImg.TarMeta[0])
fmt.Println(len(reImg.Contents[0]))
// #10: Import GoContainer from a GoImage
imCon, err := goCluster.ImportContainer("ImportedContainer", reImg)
if err != nil {
log.Fatal("Error Importing the GoContainer: ", err.Error())
}
fmt.Println(imCon.Name)
// #11: Delete GoContainer from GoCluster
err = goCluster.DeleteContainer("testContainer")
if err != nil {
log.Fatal(err.Error())
}
// #12: Get all GoContainers in GoCluster
goCons, err := goCluster.GetContainers()
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(goCons)
}