github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/firecracker/provider.go (about)

     1  package firecracker
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sync"
     7  
     8  	firecrackersdk "github.com/firecracker-microvm/firecracker-go-sdk"
     9  
    10  	"github.com/solo-io/unik/pkg/config"
    11  	"github.com/solo-io/unik/pkg/state"
    12  )
    13  
    14  type FirecrackerProvider struct {
    15  	config config.Firecracker
    16  	state  state.State
    17  
    18  	runningMachines map[string]*firecrackersdk.Machine
    19  	mapLock         sync.RWMutex
    20  }
    21  
    22  func FirecrackerStateFile() string {
    23  	return filepath.Join(config.Internal.UnikHome, "firecracker/state.json")
    24  
    25  }
    26  func firecrackerImagesDirectory() string {
    27  	return filepath.Join(config.Internal.UnikHome, "firecracker/images/")
    28  }
    29  
    30  func firecrackerInstancesDirectory() string {
    31  	return filepath.Join(config.Internal.UnikHome, "firecracker/instances/")
    32  }
    33  
    34  func firecrackerVolumesDirectory() string {
    35  	return filepath.Join(config.Internal.UnikHome, "firecracker/volumes/")
    36  }
    37  
    38  func NewProvider(config config.Firecracker) (*FirecrackerProvider, error) {
    39  
    40  	os.MkdirAll(firecrackerImagesDirectory(), 0777)
    41  	os.MkdirAll(firecrackerInstancesDirectory(), 0777)
    42  	os.MkdirAll(firecrackerVolumesDirectory(), 0777)
    43  
    44  	p := &FirecrackerProvider{
    45  		config:          config,
    46  		state:           state.NewBasicState(FirecrackerStateFile()),
    47  		runningMachines: map[string]*firecrackersdk.Machine{},
    48  	}
    49  
    50  	return p, nil
    51  }
    52  
    53  func (p *FirecrackerProvider) WithState(state state.State) *FirecrackerProvider {
    54  	p.state = state
    55  	return p
    56  }
    57  
    58  func getImagePath(imageName string) string {
    59  	return filepath.Join(firecrackerImagesDirectory(), imageName, "boot.img")
    60  }
    61  
    62  func getVolumePath(volumeName string) string {
    63  	return filepath.Join(firecrackerVolumesDirectory(), volumeName, "data.img")
    64  }
    65  
    66  func getInstanceDir(instanceName string) string {
    67  	return filepath.Join(firecrackerInstancesDirectory(), instanceName)
    68  }
    69  
    70  func getImageDir(imageName string) string {
    71  	return filepath.Join(firecrackerImagesDirectory(), imageName)
    72  }