-
-func (boot *bootCommand) installGoProgram(ctx context.Context, srcpath string) error {
- boot.goMutex.Lock()
- defer boot.goMutex.Unlock()
- env := append([]string{"GOPATH=" + boot.libPath}, os.Environ()...)
- return boot.RunProgram(ctx, filepath.Join(boot.sourcePath, srcpath), nil, env, "go", "install")
-}
-
-func (boot *bootCommand) setupRubyEnv() error {
- boot.setupRubyOnce.Do(func() {
- buf, err := exec.Command("gem", "env", "gempath").Output() // /var/lib/arvados/.gem/ruby/2.5.0/bin:...
- if err != nil || len(buf) == 0 {
- boot.setupRubyErr = fmt.Errorf("gem env gempath: %v", err)
- }
- gempath := string(bytes.Split(buf, []byte{':'})[0])
- os.Setenv("PATH", gempath+"/bin:"+os.Getenv("PATH"))
- os.Setenv("GEM_HOME", gempath)
- os.Setenv("GEM_PATH", gempath)
- })
- return boot.setupRubyErr
-}
-
-func (boot *bootCommand) RunProgram(ctx context.Context, dir string, output io.Writer, env []string, prog string, args ...string) error {
- cmdline := fmt.Sprintf("%s", append([]string{prog}, args...))
- fmt.Fprintf(boot.stderr, "%s executing in %s\n", cmdline, dir)
- cmd := exec.Command(prog, args...)
- if output == nil {
- cmd.Stdout = boot.stderr
- } else {
- cmd.Stdout = output
- }
- cmd.Stderr = boot.stderr
- if strings.HasPrefix(dir, "/") {
- cmd.Dir = dir
- } else {
- cmd.Dir = filepath.Join(boot.sourcePath, dir)
- }
- if env != nil {
- cmd.Env = env
- }
- go func() {
- <-ctx.Done()
- cmd.Process.Signal(syscall.SIGINT)
- for range time.Tick(5 * time.Second) {
- if cmd.ProcessState != nil {
- break
- }
- ctxlog.FromContext(ctx).WithField("process", cmd.Process).Infof("waiting for child process to exit after SIGINT")
- cmd.Process.Signal(syscall.SIGINT)
- }
- }()
- err := cmd.Run()
- if err != nil {
- return fmt.Errorf("%s: error: %v", cmdline, err)
- }
- return nil
-}
-
-type component struct {
- name string
- svc arvados.Service
- cmdHandler cmd.Handler
- cmdArgs []string
- railsApp string // source dir in arvados tree, e.g., "services/api"
- goProg string // source dir in arvados tree, e.g., "services/keepstore"
- notIfTest bool // don't run this component on a test cluster
-}
-
-func (cmpt *component) Run(ctx context.Context, boot *bootCommand, stdout, stderr io.Writer) error {
- if cmpt.notIfTest && boot.clusterType == "test" {
- fmt.Fprintf(stderr, "skipping component %q\n", cmpt.name)
- <-ctx.Done()
- return nil
- }
- fmt.Fprintf(stderr, "starting component %q\n", cmpt.name)
- if cmpt.cmdHandler != nil {
- errs := make(chan error, 1)
- go func() {
- defer close(errs)
- exitcode := cmpt.cmdHandler.RunCommand(cmpt.name, cmpt.cmdArgs, bytes.NewBuffer(nil), stdout, stderr)
- if exitcode != 0 {
- errs <- fmt.Errorf("exit code %d", exitcode)
- }
- }()
- select {
- case err := <-errs:
- return err
- case <-ctx.Done():
- // cmpt.cmdHandler.RunCommand() doesn't have
- // access to our context, so it won't shut
- // down by itself. We just abandon it.
- return nil
- }
- }
- if cmpt.goProg != "" {
- return boot.RunProgram(ctx, cmpt.goProg, nil, nil, "go", "run", ".")
- }
- if cmpt.railsApp != "" {
- port := "-"
- for u := range cmpt.svc.InternalURLs {
- if _, p, err := net.SplitHostPort(u.Host); err != nil {
- return err
- } else if p != "" {
- port = p
- } else if u.Scheme == "https" {
- port = "443"
- } else {
- port = "80"
- }
- break
- }
- if port == "-" {
- return fmt.Errorf("bug: no InternalURLs for component %q: %v", cmpt.name, cmpt.svc.InternalURLs)
- }
-
- err := boot.setupRubyEnv()
- if err != nil {
- return err
- }
- var buf bytes.Buffer
- err = boot.RunProgram(ctx, cmpt.railsApp, &buf, nil, "gem", "list", "--details", "bundler")
- if err != nil {
- return err
- }
- for _, version := range []string{"1.11.0", "1.17.3", "2.0.2"} {
- if !strings.Contains(buf.String(), "("+version+")") {
- err = boot.RunProgram(ctx, cmpt.railsApp, nil, nil, "gem", "install", "--user", "bundler:1.11", "bundler:1.17.3", "bundler:2.0.2")
- if err != nil {
- return err
- }
- break
- }
- }
- err = boot.RunProgram(ctx, cmpt.railsApp, nil, nil, "bundle", "install", "--jobs", "4", "--path", filepath.Join(os.Getenv("HOME"), ".gem"))
- if err != nil {
- return err
- }
- err = boot.RunProgram(ctx, cmpt.railsApp, nil, nil, "bundle", "exec", "passenger-config", "build-native-support")
- if err != nil {
- return err
- }
- err = boot.RunProgram(ctx, cmpt.railsApp, nil, nil, "bundle", "exec", "passenger-config", "install-standalone-runtime")
- if err != nil {
- return err
- }
- err = boot.RunProgram(ctx, cmpt.railsApp, nil, nil, "bundle", "exec", "passenger-config", "validate-install")
- if err != nil {
- return err
- }
- err = boot.RunProgram(ctx, cmpt.railsApp, nil, nil, "bundle", "exec", "passenger", "start", "-p", port)
- if err != nil {
- return err
- }
- }
- return fmt.Errorf("bug: component %q has nothing to run", cmpt.name)
-}
-
-func (boot *bootCommand) autofillConfig(cfg *arvados.Config, log logrus.FieldLogger) error {
- cluster, err := cfg.GetCluster("")
- if err != nil {
- return err
- }
- port := 9000
- for _, svc := range []*arvados.Service{
- &cluster.Services.Controller,
- &cluster.Services.DispatchCloud,
- &cluster.Services.RailsAPI,
- } {
- if len(svc.InternalURLs) == 0 {
- port++
- svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{
- arvados.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port)}: arvados.ServiceInstance{},
- }
- }
- }
- if cluster.Services.Controller.ExternalURL.Host == "" {
- for k := range cluster.Services.Controller.InternalURLs {
- cluster.Services.Controller.ExternalURL = k
- }
- }
- if cluster.SystemRootToken == "" {
- cluster.SystemRootToken = randomHexString(64)
- }
- if cluster.API.RailsSessionSecretToken == "" {
- cluster.API.RailsSessionSecretToken = randomHexString(64)
- }
- if cluster.Collections.BlobSigningKey == "" {
- cluster.Collections.BlobSigningKey = randomHexString(64)
- }
- if boot.clusterType != "production" && cluster.Containers.DispatchPrivateKey == "" {
- buf, err := ioutil.ReadFile(filepath.Join(boot.sourcePath, "lib", "dispatchcloud", "test", "sshkey_dispatch"))
- if err != nil {
- return err
- }
- cluster.Containers.DispatchPrivateKey = string(buf)
- }
- cfg.Clusters[cluster.ClusterID] = *cluster
- return nil
-}
-
-func randomHexString(chars int) string {
- b := make([]byte, chars/2)
- _, err := rand.Read(b)
- if err != nil {
- panic(err)
- }
- return fmt.Sprintf("%x", b)
-}