1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.arvados.org/arvados.git/lib/cmd"
23 "git.arvados.org/arvados.git/sdk/go/ctxlog"
27 var Command cmd.Handler = installCommand{}
29 const devtestDatabasePassword = "insecure_arvados_test"
31 type installCommand struct{}
33 func (installCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
34 logger := ctxlog.New(stderr, "text", "info")
35 ctx := ctxlog.Context(context.Background(), logger)
36 ctx, cancel := context.WithCancel(ctx)
42 logger.WithError(err).Info("exiting")
46 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
47 flags.SetOutput(stderr)
48 versionFlag := flags.Bool("version", false, "Write version information to stdout and exit 0")
49 clusterType := flags.String("type", "production", "cluster `type`: development, test, or production")
50 err = flags.Parse(args)
51 if err == flag.ErrHelp {
54 } else if err != nil {
56 } else if *versionFlag {
57 return cmd.Version.RunCommand(prog, args, stdin, stdout, stderr)
60 var dev, test, prod bool
69 err = fmt.Errorf("invalid cluster type %q (must be 'development', 'test', or 'production')", *clusterType)
74 err = errors.New("production install is not yet implemented")
78 osv, err := identifyOS()
83 listdir, err := os.Open("/var/lib/apt/lists")
85 logger.Warnf("error while checking whether to run apt-get update: %s", err)
86 } else if names, _ := listdir.Readdirnames(1); len(names) == 0 {
87 // Special case for a base docker image where the
88 // package cache has been deleted and all "apt-get
89 // install" commands will fail unless we fetch repos.
90 cmd := exec.CommandContext(ctx, "apt-get", "update")
108 "daemontools", // lib/boot uses setuidgid to drop privileges when running as root
109 "default-jdk-headless",
110 "default-jre-headless",
119 "libcrypt-ssleay-perl",
120 "libcrypt-ssleay-perl",
122 "libcurl4-openssl-dev",
143 "postgresql-contrib",
146 "python3-virtualenv",
156 case osv.Debian && osv.Major >= 10:
157 debs = append(debs, "libcurl4")
159 debs = append(debs, "libcurl3")
161 cmd := exec.CommandContext(ctx, "apt-get", "install", "--yes", "--no-install-recommends")
162 cmd.Args = append(cmd.Args, debs...)
163 cmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive")
172 os.Mkdir("/var/lib/arvados", 0755)
173 rubyversion := "2.5.7"
174 if haverubyversion, err := exec.Command("/var/lib/arvados/bin/ruby", "-v").CombinedOutput(); err == nil && bytes.HasPrefix(haverubyversion, []byte("ruby "+rubyversion)) {
175 logger.Print("ruby " + rubyversion + " already installed")
178 mkdir -p /var/lib/arvados/tmp
179 tmp=/var/lib/arvados/tmp/ruby-`+rubyversion+`
180 trap "rm -r ${tmp}" ERR
181 wget --progress=dot:giga -O- https://cache.ruby-lang.org/pub/ruby/2.5/ruby-`+rubyversion+`.tar.gz | tar -C /var/lib/arvados/tmp -xzf -
183 ./configure --disable-install-doc --prefix /var/lib/arvados
186 /var/lib/arvados/bin/gem install bundler
196 if havegoversion, err := exec.Command("/usr/local/bin/go", "version").CombinedOutput(); err == nil && bytes.HasPrefix(havegoversion, []byte("go version go"+goversion+" ")) {
197 logger.Print("go " + goversion + " already installed")
201 wget --progress=dot:giga -O- https://storage.googleapis.com/golang/go`+goversion+`.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
202 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
209 pjsversion := "1.9.8"
210 if havepjsversion, err := exec.Command("/usr/local/bin/phantomjs", "--version").CombinedOutput(); err == nil && string(havepjsversion) == "1.9.8\n" {
211 logger.Print("phantomjs " + pjsversion + " already installed")
214 PJS=phantomjs-`+pjsversion+`-linux-x86_64
215 wget --progress=dot:giga -O- https://bitbucket.org/ariya/phantomjs/downloads/$PJS.tar.bz2 | tar -C /var/lib/arvados -xjf -
216 ln -sf /var/lib/arvados/$PJS/bin/phantomjs /usr/local/bin/
223 geckoversion := "0.24.0"
224 if havegeckoversion, err := exec.Command("/usr/local/bin/geckodriver", "--version").CombinedOutput(); err == nil && strings.Contains(string(havegeckoversion), " "+geckoversion+" ") {
225 logger.Print("geckodriver " + geckoversion + " already installed")
229 wget --progress=dot:giga -O- https://github.com/mozilla/geckodriver/releases/download/$GD/geckodriver-$GD-linux64.tar.gz | tar -C /var/lib/arvados/bin -xzf - geckodriver
230 ln -sf /var/lib/arvados/bin/geckodriver /usr/local/bin/
237 nodejsversion := "v8.15.1"
238 if havenodejsversion, err := exec.Command("/usr/local/bin/node", "--version").CombinedOutput(); err == nil && string(havenodejsversion) == nodejsversion+"\n" {
239 logger.Print("nodejs " + nodejsversion + " already installed")
242 NJS=`+nodejsversion+`
243 wget --progress=dot:giga -O- https://nodejs.org/dist/${NJS}/node-${NJS}-linux-x64.tar.xz | sudo tar -C /var/lib/arvados -xJf -
244 ln -sf /var/lib/arvados/node-${NJS}-linux-x64/bin/{node,npm} /usr/local/bin/
251 gradleversion := "5.3.1"
252 if havegradleversion, err := exec.Command("/usr/local/bin/gradle", "--version").CombinedOutput(); err == nil && strings.Contains(string(havegradleversion), "Gradle "+gradleversion+"\n") {
253 logger.Print("gradle " + gradleversion + " already installed")
257 mkdir -p /var/lib/arvados/tmp
258 zip=/var/lib/arvados/tmp/gradle-${G}-bin.zip
260 wget --progress=dot:giga -O${zip} https://services.gradle.org/distributions/gradle-${G}-bin.zip
261 unzip -o -d /var/lib/arvados ${zip}
262 ln -sf /var/lib/arvados/gradle-${G}/bin/gradle /usr/local/bin/
270 // The entry in /etc/locale.gen is "en_US.UTF-8"; once
271 // it's installed, locale -a reports it as
273 wantlocale := "en_US.UTF-8"
274 if havelocales, err := exec.Command("locale", "-a").CombinedOutput(); err == nil && bytes.Contains(havelocales, []byte(strings.Replace(wantlocale+"\n", "UTF-", "utf", 1))) {
275 logger.Print("locale " + wantlocale + " already installed")
277 err = runBash(`sed -i 's/^# *\(`+wantlocale+`\)/\1/' /etc/locale.gen && locale-gen`, stdout, stderr)
292 if pg_lsclusters, err2 := exec.Command("pg_lsclusters", "--no-header").CombinedOutput(); err2 != nil {
293 err = fmt.Errorf("pg_lsclusters: %s", err2)
295 } else if pgclusters := strings.Split(strings.TrimSpace(string(pg_lsclusters)), "\n"); len(pgclusters) != 1 {
296 logger.Warnf("pg_lsclusters returned %d postgresql clusters -- skipping postgresql initdb/startup, hope that's ok", len(pgclusters))
297 } else if _, err = fmt.Sscanf(pgclusters[0], "%s %s %d %s %s %s %s", &pgc.Version, &pgc.Cluster, &pgc.Port, &pgc.Status, &pgc.Owner, &pgc.DataDirectory, &pgc.LogFile); err != nil {
298 err = fmt.Errorf("error parsing pg_lsclusters output: %s", err)
300 } else if pgc.Status == "online" {
301 logger.Infof("postgresql cluster %s-%s is online", pgc.Version, pgc.Cluster)
303 logger.Infof("postgresql cluster %s-%s is %s; trying to start", pgc.Version, pgc.Cluster, pgc.Status)
304 cmd := exec.Command("pg_ctlcluster", "--foreground", pgc.Version, pgc.Cluster, "start")
312 cmd.Process.Signal(syscall.SIGTERM)
313 logger.Info("sent SIGTERM; waiting for postgres to shut down")
316 err = waitPostgreSQLReady()
322 if os.Getpid() == 1 {
323 // We are the init process (presumably in a
324 // docker container) so although postgresql is
325 // installed, it's not running, and initdb
326 // might never have been run.
329 var needcoll []string
330 // If the en_US.UTF-8 locale wasn't installed when
331 // postgresql initdb ran, it needs to be added
332 // explicitly before we can use it in our test suite.
333 for _, collname := range []string{"en_US", "en_US.UTF-8"} {
334 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-t", "-c", "SELECT 1 FROM pg_catalog.pg_collation WHERE collname='"+collname+"' AND collcollate IN ('en_US.UTF-8', 'en_US.utf8')")
336 out, err2 := cmd.CombinedOutput()
338 err = fmt.Errorf("error while checking postgresql collations: %s", err2)
341 if strings.Contains(string(out), "1") {
342 logger.Infof("postgresql supports collation %s", collname)
344 needcoll = append(needcoll, collname)
347 if len(needcoll) > 0 && os.Getpid() != 1 {
348 // In order for the CREATE COLLATION statement
349 // below to work, the locale must have existed
350 // when PostgreSQL started up. If we're
351 // running as init, we must have started
352 // PostgreSQL ourselves after installing the
353 // locales. Otherwise, it might need a
354 // restart, so we attempt to restart it with
356 if err = runBash(`sudo systemctl restart postgresql`, stdout, stderr); err != nil {
357 logger.Warn("`systemctl restart postgresql` failed; hoping postgresql does not need to be restarted")
358 } else if err = waitPostgreSQLReady(); err != nil {
362 for _, collname := range needcoll {
363 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "CREATE COLLATION \""+collname+"\" (LOCALE = \"en_US.UTF-8\")")
369 err = fmt.Errorf("error adding postgresql collation %s: %s", collname, err)
374 withstuff := "WITH LOGIN SUPERUSER ENCRYPTED PASSWORD " + pq.QuoteLiteral(devtestDatabasePassword)
375 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "ALTER ROLE arvados "+withstuff)
377 if err := cmd.Run(); err == nil {
378 logger.Print("arvados role exists; superuser privileges added, password updated")
380 cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "CREATE ROLE arvados "+withstuff)
394 type osversion struct {
400 func identifyOS() (osversion, error) {
402 f, err := os.Open("/etc/os-release")
408 kv := map[string]string{}
409 scanner := bufio.NewScanner(f)
411 line := strings.TrimSpace(scanner.Text())
412 if strings.HasPrefix(line, "#") {
415 toks := strings.SplitN(line, "=", 2)
417 return osv, fmt.Errorf("invalid line in /etc/os-release: %q", line)
420 v := strings.Trim(toks[1], `"`)
422 v = strings.Trim(v, `'`)
426 if err = scanner.Err(); err != nil {
435 return osv, fmt.Errorf("unsupported ID in /etc/os-release: %q", kv["ID"])
437 vstr := kv["VERSION_ID"]
438 if i := strings.Index(vstr, "."); i > 0 {
441 osv.Major, err = strconv.Atoi(vstr)
443 return osv, fmt.Errorf("incomprehensible VERSION_ID in /etc/os-release: %q", kv["VERSION_ID"])
448 func waitPostgreSQLReady() error {
449 for deadline := time.Now().Add(10 * time.Second); ; {
450 output, err := exec.Command("pg_isready").CombinedOutput()
453 } else if time.Now().After(deadline) {
454 return fmt.Errorf("timed out waiting for pg_isready (%q)", output)
456 time.Sleep(time.Second)
461 func runBash(script string, stdout, stderr io.Writer) error {
462 cmd := exec.Command("bash", "-")
463 cmd.Stdin = bytes.NewBufferString("set -ex -o pipefail\n" + script)