feat(scaffolder): Starting to add the cookiecutter support for running the template after we've got all the data

This commit is contained in:
blam
2020-02-06 16:48:37 +01:00
parent 98dfb1afc9
commit b844de0cd5
6 changed files with 63 additions and 15 deletions
+11 -1
View File
@@ -1,3 +1,4 @@
### Builder Stage
FROM golang:1.13 AS build
ARG service
@@ -13,7 +14,9 @@ COPY $service/ /build/$service/
RUN go build .
FROM debian:buster
### Running Container Stage
FROM debian:buster AS run
ARG service
WORKDIR /app/
@@ -21,3 +24,10 @@ WORKDIR /app/
COPY --from=build /build/$service/$service /app/service
CMD ["./service"]
### Scaffolder Stage
### needs extra cookiecutter
FROM run AS scaffolder
RUN apt-get update && apt-get install -y python-pip
RUN pip install cookiecutter==1.7.0 --index-url https://pypi.python.org/simple
+34 -11
View File
@@ -3,8 +3,11 @@ package app
import (
"context"
"fmt"
"github.com/golang/protobuf/jsonpb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
"os/exec"
identity "github.com/spotify/backstage/backend/proto/identity/v1"
pb "github.com/spotify/backstage/backend/proto/scaffolder/v1"
@@ -30,20 +33,40 @@ func NewServer() *Server {
// Create scaffolds the repo in github and then will create push to the repository
func (s *Server) Create(ctx context.Context, req *pb.CreateRequest) (*pb.CreateReply, error) {
// first create the repository with github
fmt.Sprintf("Creating repository for Component %s", req.ComponentId)
repo := remote.Repository{
Name: req.ComponentId,
Org: req.Org,
Private: req.Private,
}
if _, err := s.github.CreateRepository(repo); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("Could not create repository %s/%s", req.Org, req.ComponentId))
}
log.Printf("Creating repository for Component %s", req.ComponentId)
// repo := remote.Repository{
// Name: req.ComponentId,
// Org: req.Org,
// Private: req.Private,
// }
// if _, err := s.github.CreateRepository(repo); err != nil {
// return nil, status.Error(codes.Internal, fmt.Sprintf("Could not create repository %s/%s", req.Org, req.ComponentId))
// }
marshaller := &jsonpb.Marshaler{}
d, e := marshaller.MarshalToString(req.Metadata)
// move the template into a temporary directory
tempFolder, _ := s.fs.PrepareTemplate(fs.Template{req.TemplateId})
tempFolder, err := s.fs.PrepareTemplate(
fs.Template{
ID: req.TemplateId,
Metadata: req.Metadata,
},
)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("Could not prepare the template"))
}
log.Printf("Created temporary folder %s", tempFolder)
// TODO(blam): Probably need to wrap this in a timeout or something to avoid waiting to long
cmd := exec.Command("cookiecutter", "--no-input", "-v", tempFolder)
cmd.Dir = tempFolder
err := cmd.Run()
fmt.Println(output)
fmt.Println(err)
fmt.Sprintf("Created temporary folder %s", tempFolder)
// use git bindings to add the remote with access token and push to the directory
return nil, nil
}
+13
View File
@@ -0,0 +1,13 @@
package cutter
// Cutter is the wrapper around cookiecutter
type Cutter interface {
}
type Template {
}
// Run runs cookiecutter in the folder provided
func (c *Cutter) Run() error {
}
+2 -2
View File
@@ -9,7 +9,8 @@ import (
)
// Filesystem Repository
type Filesystem struct{}
type Filesystem struct {
}
func file(src, dst string) error {
var err error
@@ -78,7 +79,6 @@ type Template struct {
// and return the temp location
func (f *Filesystem) PrepareTemplate(template Template) (string, error) {
tempDirectory, _ := ioutil.TempDir(os.TempDir(), template.ID)
fmt.Println(tempDirectory)
if err := dir("./templates/"+template.ID, tempDirectory); err != nil {
return "", err
}
+2
View File
@@ -25,6 +25,7 @@ services:
container_name: boss-builds
build:
context: backend
target: run
args:
service: builds
restart: unless-stopped
@@ -34,6 +35,7 @@ services:
container_name: boss-scaffolder
build:
context: backend
target: scaffolder
args:
service: scaffolder
restart: unless-stopped
+1 -1
View File
@@ -1,3 +1,3 @@
FROM python:3.8-slim
FROM python:3.8-slim AS scaffolder
RUN pip install cookiecutter==1.7.0 --index-url https://pypi.python.org/simple
ENTRYPOINT [ "cookiecutter" ]