Skip to content

Commit

Permalink
Just a lot of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcc committed Nov 25, 2023
1 parent fbfc632 commit 142416e
Show file tree
Hide file tree
Showing 36 changed files with 801 additions and 272 deletions.
8 changes: 4 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ builds:
env:
- CGO_ENABLED=0
targets:
- windows_amd64
- windows_arm64
# - windows_amd64
# - windows_arm64
- darwin_amd64
- darwin_arm64
- linux_amd64
Expand All @@ -22,8 +22,8 @@ builds:
env:
- CGO_ENABLED=0
targets:
- windows_amd64
- windows_arm64
# - windows_amd64
# - windows_arm64
- darwin_amd64
- darwin_arm64
- linux_amd64
Expand Down
69 changes: 69 additions & 0 deletions app/backend/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package appbackend

import (
"fmt"
"github.com/graphql-go/graphql"
)

var packageType = graphql.NewObject(graphql.ObjectConfig{
Name: "Package",
Fields: graphql.Fields{
"goos": &graphql.Field{Type: graphql.NewNonNull(graphql.String)},
"goarch": &graphql.Field{Type: graphql.NewNonNull(graphql.String)},
"version": &graphql.Field{Type: graphql.NewNonNull(graphql.String)},
"goarm": &graphql.Field{Type: graphql.String},
},
})

var listPackages = &graphql.Field{
Type: graphql.NewList(packageType),
Args: graphql.FieldConfigArgument{},
Resolve: wrapper(func(rctx resolveContext[any]) (any, error) {
return rctx.packages.GetLatestPackages(rctx)
}),
}

var buildDeployAgentCommand = &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
Args: graphql.FieldConfigArgument{
"projectId": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String)},
"goos": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String)},
"goarch": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String)},
"generatePsk": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.Boolean)},
},
Resolve: wrapper(func(rctx resolveContext[any]) (string, error) {
goos := rctx.getStringArg("goos")
projectId := rctx.getStringArg("projectId")
err := rctx.canAccessProject(projectId)
if err != nil {
return "", err
}

// Optionally generate a pre-shared key for the agent to use
var psk string
if rctx.getBoolArg("generatePsk") {
psk, err = rctx.db.GeneratePreSharedKey(rctx, projectId)
if err != nil {
return "", fmt.Errorf("generating psk: %w", err)
}
}

switch goos {
case "linux":
// We can use the same command for all linux architectures
// and the installation script will handle the nuances and
// platform detection processes that need to happen.
return buildLinuxInstallCommand(psk), nil
default:
return "", fmt.Errorf("unsupported goos: %s", goos)
}
}),
}

func buildLinuxInstallCommand(psk string) string {
if len(psk) == 0 {
return fmt.Sprintf("curl -fsSL https://raw.githubusercontent.com/clarkmcc/cloudcore/main/scripts/linux/install.sh | sh")
} else {
return fmt.Sprintf("curl -fsSL https://raw.githubusercontent.com/clarkmcc/cloudcore/main/scripts/linux/install.sh | sh -s -- --psk %s", psk)
}
}
5 changes: 4 additions & 1 deletion app/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/clarkmcc/cloudcore/app/backend/middleware"
"github.com/clarkmcc/cloudcore/cmd/cloudcore-server/config"
"github.com/clarkmcc/cloudcore/cmd/cloudcore-server/database"
"github.com/clarkmcc/cloudcore/pkg/packages"
"github.com/gin-gonic/gin"
"github.com/graphql-go/graphql"
"go.uber.org/fx"
Expand All @@ -19,9 +20,10 @@ type Server struct {
schema graphql.Schema
logger *zap.Logger
database database.Database
packages packages.Provider
}

func New(lc fx.Lifecycle, config *config.Config, database database.Database, logger *zap.Logger) (*Server, error) {
func New(lc fx.Lifecycle, config *config.Config, database database.Database, logger *zap.Logger, packages packages.Provider) (*Server, error) {
logger = logger.Named("app-backend")
s, err := graphql.NewSchema(schemaConfig)
if err != nil {
Expand All @@ -31,6 +33,7 @@ func New(lc fx.Lifecycle, config *config.Config, database database.Database, log
schema: s,
logger: logger,
database: database,
packages: packages,
}

r := gin.Default()
Expand Down
43 changes: 22 additions & 21 deletions app/backend/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ import (
"fmt"
"github.com/clarkmcc/cloudcore/app/backend/middleware"
"github.com/clarkmcc/cloudcore/cmd/cloudcore-server/database"
"github.com/clarkmcc/cloudcore/pkg/packages"
"github.com/graphql-go/graphql"
"github.com/spf13/cast"
"go.uber.org/zap"
)

const contextKeyLogger = "logger"
const contextKeyDatabase = "database"
type contextKeyLogger struct{}
type contextKeyDatabase struct{}
type contextKeyPackages struct{}

func (s *Server) graphqlContext(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, contextKeyLogger, s.logger)
ctx = context.WithValue(ctx, contextKeyDatabase, s.database)
ctx = context.WithValue(ctx, contextKeyLogger{}, s.logger)
ctx = context.WithValue(ctx, contextKeyDatabase{}, s.database)
ctx = context.WithValue(ctx, contextKeyPackages{}, s.packages)
return ctx
}

func logger(ctx context.Context) *zap.Logger {
return ctx.Value(contextKeyLogger).(*zap.Logger)
}

func db(ctx context.Context) database.Database {
return ctx.Value(contextKeyDatabase).(database.Database)
}

type resolveContext[S any] struct {
context.Context
db database.Database
params graphql.ResolveParams
logger *zap.Logger
source S
db database.Database
params graphql.ResolveParams
logger *zap.Logger
packages packages.Provider
source S
}

func (r *resolveContext[S]) getStringArg(name string) string {
return cast.ToString(r.params.Args[name])
}

func (r *resolveContext[S]) getBoolArg(name string) bool {
return cast.ToBool(r.params.Args[name])
}

func (r *resolveContext[S]) canAccessProject(projectId string) error {
sub := middleware.SubjectFromContext(r)
if sub == "" {
Expand All @@ -65,11 +65,12 @@ func wrapper[S any, T any](fn resolverFunc[T, S]) func(params graphql.ResolvePar
return nil, fmt.Errorf("invalid source type: %T, expected %T", params.Source, s)
}
return fn(resolveContext[S]{
Context: ctx,
db: db(ctx),
logger: logger(ctx),
source: source,
params: params,
Context: ctx,
db: ctx.Value(contextKeyDatabase{}).(database.Database),
packages: ctx.Value(contextKeyPackages{}).(packages.Provider),
logger: ctx.Value(contextKeyLogger{}).(*zap.Logger),
source: source,
params: params,
})
}
}
6 changes: 4 additions & 2 deletions app/backend/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ var schemaConfig = graphql.SchemaConfig{
"hosts": hostList,
"host": hostDetails,
"projectMetrics": projectMetrics,
"packages": listPackages,
},
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{
"ensureUser": ensureUser,
"projectCreate": projectCreate,
"ensureUser": ensureUser,
"projectCreate": projectCreate,
"buildDeployAgentCommand": buildDeployAgentCommand,
},
}),
}
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"react-dom": "^18.2.0",
"react-hook-form": "^7.48.2",
"react-router-dom": "^6.19.0",
"react-usage-bar": "^1.1.22",
"react-usage-bar": "^1.2.0",
"sort-by": "^1.2.0",
"tailwind-merge": "^2.0.0",
"tailwindcss-animate": "^1.0.7",
Expand Down
9 changes: 9 additions & 0 deletions app/frontend/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Host {
}

type Mutation {
buildDeployAgentCommand(generatePsk: Boolean!, goarch: String!, goos: String!, projectId: String!): String!
ensureUser: [Project]
projectCreate(description: String, name: String!): ProjectCreate
}
Expand All @@ -56,6 +57,13 @@ type OsNameCount {
osName: String!
}

type Package {
goarch: String!
goarm: String
goos: String!
version: String!
}

type Project {
created_at: DateTime
id: String
Expand All @@ -80,6 +88,7 @@ type ProjectMetrics {
type Query {
host(hostId: String!, projectId: String!): Host
hosts(projectId: String!): [Host]
packages: [Package]
projectMetrics(projectId: String!): ProjectMetrics
}

Expand Down
27 changes: 27 additions & 0 deletions app/frontend/src/components/command-copy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState } from "react";
import { ClipboardCopy } from "lucide-react";

export function CommandCopy(props: { command: string }) {
const [copied, setCopied] = useState(false);

const handleCopy = async () => {
await navigator.clipboard.writeText(props.command);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};

return (
<div className="relative p-4 bg-neutral-100 dark:bg-neutral-900 dark:text-white font-mono text-sm rounded-md overflow-auto">
<div className="overflow-x-auto whitespace-nowrap scroll-auto">
{props.command}
</div>
<button
onClick={handleCopy}
className="absolute top-2 right-2 flex items-center gap-2 p-1 text-xs bg-neutral-200 hover:bg-neutral-300 dark:bg-neutral-700 dark:hover:bg-neutral-600 rounded"
>
<ClipboardCopy className="h-4 w-4" />
{copied ? "Copied" : "Copy"}
</button>
</div>
);
}
Loading

0 comments on commit 142416e

Please sign in to comment.