Skip to content

Commit

Permalink
Merge branch 'infrastructure/#213-local-daemon' into daemon/#174-webs…
Browse files Browse the repository at this point in the history
…ockets
  • Loading branch information
bobheadxi committed May 28, 2018
2 parents 0308942 + 3a9098a commit aca0d05
Show file tree
Hide file tree
Showing 44 changed files with 90 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ result, _ := remote.RunSSHCommand(string(shellScriptData))

### Daemon

The Inertia daemon package manages all serverside functionality and is the core of the Inertia platform. The daemon codebase is in `./daemon/inertia/`.
The Inertia daemon package manages all serverside functionality and is the core of the Inertia platform. The daemon codebase is in `./daemon/inertiad/`.

To locally test a daemon compiled from source, set your Inertia version in `.inertia.toml` to `test` and run:

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
*.dylib
/inertia.*
/inertia
/inertiad
/bin

# Assets
users.db

# Test binary, build with `go test -c`
*.test

Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ RUN if [ ! -d "vendor" ]; then \
dep ensure; \
fi
# Build daemon binary.
RUN go build -o /bin/inertia \
RUN go build -o /bin/inertiad \
-ldflags "-X main.Version=$INERTIA_VERSION" \
./daemon/inertia
./daemon/inertiad

### Part 3 - Copy builds into combined image
FROM alpine
LABEL maintainer "UBC Launchpad team@ubclaunchpad.com"
WORKDIR /app
COPY --from=daemon-build-env /bin/inertia /usr/local/bin
COPY --from=daemon-build-env /bin/inertiad /usr/local/bin
COPY --from=web-build-env \
/go/src/github.com/ubclaunchpad/inertia/daemon/web/public/ \
/app/inertia-web

# Serve the daemon by default.
ENTRYPOINT ["inertia", "run"]
ENTRYPOINT ["inertiad", "run"]
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ testdaemon:
root@0.0.0.0:/daemon-image
rm -f ./inertia-daemon-image

# Run a test daemon locally
.PHONY: localdaemon
localdaemon:
bash ./test/start_local_daemon.sh

# Creates a daemon release and pushes it to Docker Hub repository.
# Requires access to the UBC Launch Pad Docker Hub.
.PHONY: daemon
Expand Down
10 changes: 5 additions & 5 deletions client/bootstrap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/bootstrap/token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ sudo docker run --rm \
-v $HOME:/app/host \
-e SSH_KNOWN_HOSTS='/app/host/.ssh/known_hosts' \
-e HOME=$HOME \
--entrypoint=inertia \
--entrypoint=inertiad \
ubclaunchpad/inertia:$RELEASE token
4 changes: 2 additions & 2 deletions daemon/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Daemon

[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://godoc.org/github.com/ubclaunchpad/inertia/daemon/)
[![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](https://godoc.org/github.com/ubclaunchpad/inertiad/daemon/)

This directory contains the Inertia daemon's two components:

- `./inertia/` is the daemon server, which handles requests and controls deployment
- `./inertiad/` is the daemon server, which handles requests and controls deployment

- `./web/` is the Inertia Web application served by the daemon
4 changes: 3 additions & 1 deletion daemon/inertia/auth/auth.go → daemon/inertiad/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)

const (
var (
// DaemonGithubKeyLocation is the default path of the deploy key
DaemonGithubKeyLocation = "/app/host/.ssh/id_rsa_inertia_deploy"
)

const (
tokenInvalidErrorMsg = "Token invalid"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// UserDatabasePath is the default location for storing users.
const UserDatabasePath = "/app/host/.inertia/users.db"
var UserDatabasePath = "/app/host/.inertia/users.db"

// PermissionsHandler handles users, permissions, and sessions on top
// of an http.ServeMux. It is used for Inertia Web.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 12 additions & 3 deletions daemon/inertia/cmd.go → daemon/inertiad/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/ubclaunchpad/inertia/daemon/inertia/auth"
"github.com/ubclaunchpad/inertia/daemon/inertiad/auth"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -16,7 +16,7 @@ var Version string
// runCmd starts the daemon
var runCmd = &cobra.Command{
Version: getVersion(),
Use: "run",
Use: "run [host] [key path] [ssl directory] [userdb dir]",
Short: "Run the daemon",
Long: `Run the daemon on a port, default 4303. Requires
host address as an argument.
Expand All @@ -29,7 +29,12 @@ Example:
if err != nil {
log.WithError(err)
}
run(args[0], port, Version)

if len(args) == 4 {
run(args[0], port, Version, args[1], args[2], args[3])
} else {
run(args[0], port, Version, "", "", "")
}
},
}

Expand All @@ -40,6 +45,10 @@ var tokenCmd = &cobra.Command{
Long: `Produce an API token to use with the daemon,
Created using an RSA private key.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
auth.DaemonGithubKeyLocation = args[0]
}

keyBytes, err := auth.GetAPIPrivateKey(nil)
if err != nil {
log.Fatal(err)
Expand Down
37 changes: 26 additions & 11 deletions daemon/inertia/daemon.go → daemon/inertiad/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package main

import (
"context"
"fmt"
"net/http"
"os"
"path"
"sync"

"github.com/docker/docker/api/types"
docker "github.com/docker/docker/client"
"github.com/gorilla/websocket"
"github.com/ubclaunchpad/inertia/daemon/inertia/auth"
"github.com/ubclaunchpad/inertia/daemon/inertia/project"
"github.com/ubclaunchpad/inertia/daemon/inertiad/auth"
"github.com/ubclaunchpad/inertia/daemon/inertiad/project"
)

var (
Expand All @@ -24,18 +26,32 @@ var (
socketUpgrader = websocket.Upgrader{}
)

var (
// specify location of SSL certificate
sslDirectory = "/app/host/.inertia/.ssl/"
)

const (
msgNoDeployment = "No deployment is currently active on this remote - try running 'inertia $REMOTE up'"

// specify location of SSL certificate
sslDirectory = "/app/host/.inertia/.ssl/"
daemonSSLCert = sslDirectory + "daemon.cert"
daemonSSLKey = sslDirectory + "daemon.key"
)

// run starts the daemon
func run(host, port, version string) {
func run(host, port, version, keyPath, certDir, userDir string) {
daemonVersion = version
if keyPath != "" {
auth.DaemonGithubKeyLocation = keyPath
}
if certDir != "" {
sslDirectory = certDir
}
if userDir != "" {
auth.UserDatabasePath = userDir
}

var (
daemonSSLCert = path.Join(sslDirectory, "daemon.cert")
daemonSSLKey = path.Join(sslDirectory, "daemon.key")
)

// Download build tools
cli, err := docker.NewEnvClient()
Expand All @@ -53,10 +69,9 @@ func run(host, port, version string) {
certNotPresent := os.IsNotExist(err)
_, err = os.Stat(daemonSSLKey)
keyNotPresent := os.IsNotExist(err)
sslRequirementsPresent := !(keyNotPresent && certNotPresent)

// If they are not available, generate new ones.
if !sslRequirementsPresent {
if keyNotPresent && certNotPresent {
println("No certificates found - generating new ones...")
err = auth.GenerateCertificate(daemonSSLCert, daemonSSLKey, host+":"+port, "RSA")
if err != nil {
Expand Down Expand Up @@ -100,7 +115,7 @@ func run(host, port, version string) {

// Serve daemon on port
println("Serving daemon on port " + port)
println(http.ListenAndServeTLS(
fmt.Println(http.ListenAndServeTLS(
":"+port,
daemonSSLCert,
daemonSSLKey,
Expand Down
4 changes: 2 additions & 2 deletions daemon/inertia/down.go → daemon/inertiad/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"

docker "github.com/docker/docker/client"
"github.com/ubclaunchpad/inertia/daemon/inertia/log"
"github.com/ubclaunchpad/inertia/daemon/inertia/project"
"github.com/ubclaunchpad/inertia/daemon/inertiad/log"
"github.com/ubclaunchpad/inertia/daemon/inertiad/project"
)

// downHandler tries to take the deployment offline
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions daemon/inertia/logs.go → daemon/inertiad/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

docker "github.com/docker/docker/client"
"github.com/gorilla/websocket"
"github.com/ubclaunchpad/inertia/daemon/inertia/log"
"github.com/ubclaunchpad/inertia/daemon/inertia/project"
"github.com/ubclaunchpad/inertia/daemon/inertiad/log"
"github.com/ubclaunchpad/inertia/daemon/inertiad/project"
)

// logHandler handles requests for container logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

docker "github.com/docker/docker/client"
"github.com/ubclaunchpad/inertia/common"
"github.com/ubclaunchpad/inertia/daemon/inertia/project"
"github.com/ubclaunchpad/inertia/daemon/inertiad/project"
)

// This file contains mock implementations of interfaces used by this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
docker "github.com/docker/docker/client"
"github.com/ubclaunchpad/inertia/daemon/inertia/log"
"github.com/ubclaunchpad/inertia/daemon/inertiad/log"
)

const (
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

docker "github.com/docker/docker/client"
"github.com/ubclaunchpad/inertia/common"
"github.com/ubclaunchpad/inertia/daemon/inertia/auth"
"github.com/ubclaunchpad/inertia/daemon/inertiad/auth"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/ubclaunchpad/inertia/common"
"github.com/ubclaunchpad/inertia/daemon/inertia/auth"
"github.com/ubclaunchpad/inertia/daemon/inertiad/auth"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion daemon/inertia/reset.go → daemon/inertiad/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

docker "github.com/docker/docker/client"
"github.com/ubclaunchpad/inertia/daemon/inertia/log"
"github.com/ubclaunchpad/inertia/daemon/inertiad/log"
)

// resetHandler shuts down and wipes the project directory
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions daemon/inertia/up.go → daemon/inertiad/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

docker "github.com/docker/docker/client"
"github.com/ubclaunchpad/inertia/common"
"github.com/ubclaunchpad/inertia/daemon/inertia/auth"
"github.com/ubclaunchpad/inertia/daemon/inertia/log"
"github.com/ubclaunchpad/inertia/daemon/inertia/project"
"github.com/ubclaunchpad/inertia/daemon/inertiad/auth"
"github.com/ubclaunchpad/inertia/daemon/inertiad/log"
"github.com/ubclaunchpad/inertia/daemon/inertiad/project"
)

// upHandler tries to bring the deployment online
Expand Down
2 changes: 1 addition & 1 deletion daemon/inertia/webhook.go → daemon/inertiad/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
docker "github.com/docker/docker/client"
"github.com/google/go-github/github"
"github.com/ubclaunchpad/inertia/common"
"github.com/ubclaunchpad/inertia/daemon/inertia/project"
"github.com/ubclaunchpad/inertia/daemon/inertiad/project"
)

var webhookSecret = "inertia"
Expand Down
11 changes: 11 additions & 0 deletions test/start_local_daemon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

# Script for running an Inertia daemon locally.
go build ./daemon/inertiad

echo "Daemon Token:" $(./inertiad token $(pwd)/test/keys/id_rsa)

./inertiad run 127.0.0.1 \
$(pwd)/test/keys/id_rsa \
$(pwd)/test/certs/ \
$(pwd)/test/users.db
2 changes: 2 additions & 0 deletions test/start_vps.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/sh

# Script for starting up testvps and neatly outputting
# information about the testvps

Expand Down

0 comments on commit aca0d05

Please sign in to comment.