Skip to content

Commit

Permalink
add new command to freeze environments
Browse files Browse the repository at this point in the history
does everything but the tarball
  • Loading branch information
justone committed Jun 19, 2018
1 parent 3ac7d9f commit 010b989
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
56 changes: 54 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -108,8 +110,7 @@ func ParsePorts(portSpecs []string) ([]Port, error) {
return ports, nil
}

func DestroyEnvironment(dc DockerClient, sc SystemClient, envName string) error {

func DestroyContainer(dc DockerClient, sc SystemClient, envName string) error {
logrus.Debugf("Stopping environment")
env, err := EnsureStopped(dc, sc, envName)
if err != nil {
Expand All @@ -123,6 +124,57 @@ func DestroyEnvironment(dc DockerClient, sc SystemClient, envName string) error
}
}

return nil
}

func FreezeEnvironment(dc DockerClient, sc SystemClient, envName string) error {

env, err := GetEnvironment(dc, sc, envName)
if err != nil {
return err
}

path, err := sc.EnsureEnvironmentDir(envName)
if err != nil {
return err
}

// save the env info into a json file
data, err := json.MarshalIndent(env, "", " ")
if err != nil {
return err
}

fmt.Println("Saving environment metadata...")
ioutil.WriteFile(filepath.Join(path, "skeg.json"), data, 0644)
if err != nil {
return err
}

// destroy the container
fmt.Println("Destroying container...")
err = DestroyContainer(dc, sc, envName)
if err != nil {
return err
}

// tarball it up
fmt.Printf("Tarballing up files...skipping tarball creation, env directory is %s.\n", path)

return nil
}

func DestroyEnvironment(dc DockerClient, sc SystemClient, envName string) error {
err := DestroyContainer(dc, sc, envName)
if err != nil {
return err
}

env, err := GetEnvironment(dc, sc, envName)
if err != nil {
return err
}

logrus.Debugf("Removing local environment directory")
err = sc.RemoveEnvironmentDir(env.Name)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions freeze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
)

type FreezeCommand struct {
Args struct {
Name string `description:"Name of environment."`
} `positional-args:"yes" required:"yes"`
}

var freezeCommand FreezeCommand

func (x *FreezeCommand) Execute(args []string) error {
dc, err := NewDockerClient(globalOptions.toConnectOpts())
if err != nil {
return err
}

sc, err := NewSystemClient()
if err != nil {
return err
}

return FreezeEnvironment(dc, sc, freezeCommand.Args.Name)
}

func init() {
_, err := parser.AddCommand("freeze",
"Freeze an environment.",
"",
&freezeCommand)

if err != nil {
fmt.Println(err)
}
}

0 comments on commit 010b989

Please sign in to comment.