Skip to content

Commit

Permalink
Merge pull request #295 from ubclaunchpad/daemon/#158-image-pruning
Browse files Browse the repository at this point in the history
Image pruning
  • Loading branch information
bobheadxi committed Jul 13, 2018
2 parents 696e3ff + 7d62818 commit 583607f
Show file tree
Hide file tree
Showing 17 changed files with 602 additions and 87 deletions.
13 changes: 8 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ func (c *Client) Up(gitRemoteURL, buildType string, stream bool) (*http.Response
})
}

// Prune clears Docker assets on this remote.
func (c *Client) Prune() (*http.Response, error) {
return c.post("/prune", nil)
}

// Down brings the project down on the remote VPS instance specified
// in the configuration object.
func (c *Client) Down() (*http.Response, error) {
Expand Down Expand Up @@ -315,18 +320,16 @@ func (c *Client) ListEnv() (*http.Response, error) {

// AddUser adds an authorized user for access to Inertia Web
func (c *Client) AddUser(username, password string, admin bool) (*http.Response, error) {
reqContent := &common.UserRequest{
return c.post("/user/adduser", &common.UserRequest{
Username: username,
Password: password,
Admin: admin,
}
return c.post("/user/adduser", reqContent)
})
}

// RemoveUser prevents a user from accessing Inertia Web
func (c *Client) RemoveUser(username string) (*http.Response, error) {
reqContent := &common.UserRequest{Username: username}
return c.post("/user/removeuser", reqContent)
return c.post("/user/removeuser", &common.UserRequest{Username: username})
}

// ResetUsers resets all users on the remote.
Expand Down
192 changes: 192 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/ubclaunchpad/inertia/cfg"
"github.com/ubclaunchpad/inertia/common"
Expand Down Expand Up @@ -238,6 +239,28 @@ func TestUp(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestPrune(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "POST", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/prune", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := getMockClient(testServer)
resp, err := d.Prune()
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestDown(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -336,3 +359,172 @@ func TestLogs(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestLogsWebsocket(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Check request method
assert.Equal(t, "GET", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/logs", endpoint)

// Check body
defer req.Body.Close()
q := req.URL.Query()
assert.Equal(t, "docker-compose", q.Get(common.Container))
assert.Equal(t, "true", q.Get(common.Stream))

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))

socketUpgrader := websocket.Upgrader{}
socket, err := socketUpgrader.Upgrade(rw, req, nil)
assert.Nil(t, err)

err = socket.WriteMessage(websocket.TextMessage, []byte("hello world"))
assert.Nil(t, err)
}))
defer testServer.Close()

d := getMockClient(testServer)
resp, err := d.LogsWebSocket("docker-compose")
assert.Nil(t, err)

time.Sleep(1 * time.Second)
_, m, err := resp.ReadMessage()
assert.Nil(t, err)
assert.Equal(t, []byte("hello world"), m)
}

func TestUpdateEnv(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "POST", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/env", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := getMockClient(testServer)
resp, err := d.UpdateEnv("", "", false, false)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestListEnv(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "GET", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/env", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := getMockClient(testServer)
resp, err := d.ListEnv()
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestAddUser(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "POST", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/user/adduser", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := getMockClient(testServer)
resp, err := d.AddUser("", "", false)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestRemoveUser(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "POST", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/user/removeuser", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := getMockClient(testServer)
resp, err := d.RemoveUser("")
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestResetUser(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "POST", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/user/resetusers", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := getMockClient(testServer)
resp, err := d.ResetUsers()
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestListUsers(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "GET", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/user/listusers", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := getMockClient(testServer)
resp, err := d.ListUsers()
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
56 changes: 35 additions & 21 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,21 @@ Run 'inertia [REMOTE] init' to gather this information.`,
up.Flags().String("type", "", "Specify a build method for your project")
cmd.AddCommand(up)

down := deepCopy(cmdDeploymentDown)
cmd.AddCommand(down)

status := deepCopy(cmdDeploymentStatus)
cmd.AddCommand(status)

logs := deepCopy(cmdDeploymentLogs)
cmd.AddCommand(logs)
cmd.AddCommand(deepCopy(cmdDeploymentDown))
cmd.AddCommand(deepCopy(cmdDeploymentStatus))
cmd.AddCommand(deepCopy(cmdDeploymentLogs))
cmd.AddCommand(deepCopy(cmdDeploymentPrune))

user := deepCopy(cmdDeploymentUser)
adduser := deepCopy(cmdDeploymentAddUser)
adduser.Flags().Bool("admin", false, "Create an admin user")
removeuser := deepCopy(cmdDeploymentRemoveUser)
resetusers := deepCopy(cmdDeploymentResetUsers)
listusers := deepCopy(cmdDeploymentListUsers)
user.AddCommand(adduser)
user.AddCommand(removeuser)
user.AddCommand(resetusers)
user.AddCommand(listusers)
user.AddCommand(deepCopy(cmdDeploymentRemoveUser))
user.AddCommand(deepCopy(cmdDeploymentResetUsers))
user.AddCommand(deepCopy(cmdDeploymentListUsers))
cmd.AddCommand(user)

ssh := deepCopy(cmdDeploymentSSH)
cmd.AddCommand(ssh)
cmd.AddCommand(deepCopy(cmdDeploymentSSH))

send := deepCopy(cmdDeploymentSendFile)
send.Flags().StringP("dest", "d", "", "Path relative from project root to send file to")
Expand All @@ -109,11 +101,8 @@ Run 'inertia [REMOTE] init' to gather this information.`,
env.AddCommand(deepCopy(cmdDeploymentEnvList))
cmd.AddCommand(env)

init := deepCopy(cmdDeploymentInit)
cmd.AddCommand(init)

reset := deepCopy(cmdDeploymentReset)
cmd.AddCommand(reset)
cmd.AddCommand(deepCopy(cmdDeploymentInit))
cmd.AddCommand(deepCopy(cmdDeploymentReset))

remove := deepCopy(cmdDeploymentRemove)
cmd.AddCommand(remove)
Expand Down Expand Up @@ -342,6 +331,31 @@ var cmdDeploymentLogs = &cobra.Command{
},
}

var cmdDeploymentPrune = &cobra.Command{
Use: "prune",
Short: "Prune Docker assets and images on your remote",
Long: `Prunes Docker assets and images from your remote to save storage space.`,
Run: func(cmd *cobra.Command, args []string) {
remoteName := strings.Split(cmd.Parent().Use, " ")[0]
inertia, _, err := local.GetClient(remoteName, configFilePath, cmd)
if err != nil {
log.Fatal(err)
}

resp, err := inertia.Prune()
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}

fmt.Printf("(Status code %d) %s\n", resp.StatusCode, body)
},
}

var cmdDeploymentSSH = &cobra.Command{
Use: "ssh",
Short: "Start an interactive SSH session",
Expand Down
Loading

0 comments on commit 583607f

Please sign in to comment.