-
Notifications
You must be signed in to change notification settings - Fork 11
/
bootstrap.go
71 lines (60 loc) · 1.75 KB
/
bootstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/runtime"
)
const (
// serviceRootFile is the file which a directory must contain to be considered a service
serviceRootFile = "main.go"
// source is the repository containing the source code
source = "github.com/micro/services"
)
func main() {
srv := micro.NewService()
srv.Init()
logger.Infof("Using %v runtime", srv.Options().Runtime)
// setup an admin account for the service to use (required to run services in a custom namespace)
// this is a temporaty solution until identity is setup then we'll need to pass a set of creds
// as arguments.
name := fmt.Sprintf("bootstrap-%v", srv.Options().Server.Options().Id)
acc, err := srv.Options().Auth.Generate(name, auth.WithScopes("admin"))
if err != nil {
logger.Fatal(err)
}
tok, err := srv.Options().Auth.Token(auth.WithCredentials(acc.ID, acc.Secret))
if err != nil {
logger.Fatal(err)
}
srv.Options().Auth.Init(auth.ClientToken(tok))
for _, name := range listServices() {
logger.Infof("Creating %v", name)
err := srv.Options().Runtime.Create(
&runtime.Service{Name: name, Source: source},
runtime.CreateNamespace("platform"),
runtime.CreateImage("docker.pkg.github.com/micro/services/"+strings.ReplaceAll(name, "/", "-")),
)
if err != nil {
logger.Fatal(err)
}
}
}
func listServices() []string {
var services []string
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Base(path) != serviceRootFile {
return nil
}
services = append(services, filepath.Dir(path))
return nil
})
return services
}